今天做一个循环创建TEXT的按钮 ,但是无法复制。初步估计是选择器的写法有问题求指点
$(function () {
var txt = ["1", "2", "3", "4", "5"]
$("#1").click(function () {
for (i = 0; i < txt.length; i++) {
tr = ' <div><input type="text" class="three3" value="ssss"/>' +
'</div>'
var $tr = $(tr);
//循环创建按钮
$("#2").append($tr);
//赋值
$(".three3 :eq(i)").val(txt[i]);
}
})
})
<body>
<button type="button" id="1">button</button>
<button type="button" id="3">button</button>
<div id="2"></div>
</body>
$(".three3 :eq(i)").val(txt[i]);
改为
$(".three3 :eq("+i+")").val(txt[i]);
================================
html
<html> <head> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script> $(function () { var txt = ["1", "2", "3", "4", "5"]; $("#1").click(function () { for (i = 0; i < txt.length; i++) { var tr = ' <div><input type="text" class="three3" value="ssss"/></div>' //循环创建按钮 $("#2").append(tr); //赋值 $(".three3 :eq("+i+")").val(txt[i]); } }) }); </script> </head> <body> <button type="button" id="1">Click</button> <button type="button" id="3">button</button> <div id="2"></div> </body> </html>