我有一个人做了这个脚本(信用Chyno Deluxe)生成一个菜单列表,无论我们在盒子上写什么,问题是,我需要生成一个连续添加到它的数字序列
这是需要的例子,
<li id='item1'></li> <li id='item2'></li> <li id='item3'></li> <li id='item4'></li>
项目’#’旁边生成的数字,1,2,3,4,5,6
我有这个生成数字,但它是固定数字,在这里
$.map($(Array(8)),function(val, i) { return i; })
这只是这样的
1,2,3,4,5,6,7,8
剧本
(function($) {
"use strict";
var t2h = {
buildHTML: function() {
var i, list, type = this.htmlSelect.options;
if (type[1].selected) {
//console.log(type[1].text);
list = "<select>";
list += "\n";
for (i = 0; i < this.get_items().length; i++) {
list += " <option>";
list += this.get_items()[i];
list += "</option>";
list += "\n";
}
list += "</select>";
您可以使用jquery代码查看下面的演示
这将产生
<select>
<option>menu 1</option>
<option>menu 2</option>
</select>
我需要通过在其上添加tag id =”数字来改进它,就像这样
<select>
<option id='item1'>menu 1</option>
<option id='item2'>menu 2</option>
</select>
演示:[链接] http://codepen.io/diden/pen/YwwVKO
希望我能在这里得到帮助,谢谢你们:)
最佳答案
for (i = 0; i < this.get_items().length; i++) { // here i will go from 0 to the length of the items the select will have -1
list += " <option>"; // for each of these values of i, we add the first part of the html to list, which is a string variable
list += this.get_items()[i]; // this adds the ith text that the user wrote, but it´s 0 index based instead of starting with 1
所以你想要的只是添加与用户输入的行索引相关的id.而这只是我1!
所以:
list += " <option id='item"+(i+1)+"'>"; // we open open and close "" to add (i+1) which is a varibale, and its the id we wanted