我想将jquery-ui微调器应用于动态添加的元素。
- HTML代码 -
<table>
<tr>
<td>
<div id="eachBox1"></div>
</td>
<td>
<div id="eachBox2"></div>
...
</table>
- jquery代码 -
$.ajax({
...
success: function (items) {
$.each(items.d, function (idx, item) {
$('#eachBox'+idx).html
( ...
'<input type="text" id="qty'+idx+'" value="1"/>'
...
)
}
}
})
我希望将jquery-ui微调器应用于名称以qty开头的每个元素。 所以我试过
$(document).on("load","[id^=qty]" function () {
$(this).spinner();
});
但它不起作用。如果我将load
事件更改为click
,则可行。
但这不是我想要的。
你有任何解决方案吗?
答案 0 :(得分:0)
我认为,您的代码中遗漏了逗号。
$(document).on("load","[id^=qty]", function () { $(this).spinner(); });
这段代码怎么样?
$(document).ready(function(){
$("[id^=qty]").spinner();
});
答案 1 :(得分:0)
试试这个:
$.ajax({
...
success: function (items) {
$.each(items.d, function (idx, item) {
$('#eachBox'+idx).html
( ...
'<input type="text" id="qty'+idx+'" value="1"/>'
...
)
}
$("[id^=qty]").spinner();
}
})
只需将.spinner()
添加到success: function(){}
,以便仅在创建元素后应用spinner()
。
答案 2 :(得分:0)
试试这个
$.ajax({
...
success: function (items) {
$.each(items.d, function (idx, item) {
$('#eachBox'+idx).html
( ...
'<input type="text" id="qty'+idx+'" value="1"/>'
...
)
$('#qty'+idx).spinner();
}
}
})
我希望这会有用