我试图实现复选框选中事件,但似乎无法将其触发到添加到列表中的新元素。这是jquery代码。$("#button").click(function() { var toAdd = $("input[name=ListItem]").val(); $("ul").append( `<li class="" id=` + generateID() + `><input name="name" type="checkbox" class="name" />` + listName + `</li>` ); }); $('input[type="checkbox"]').click(function() { if ($(this).prop("checked") == true) { alert("Checkbox is checked."); } else if ($(this).prop("checked") == false) { alert("Checkbox is unchecked."); } });<ol><li id="1" class=""><input name="name" type="checkbox" class="name"> List Name1 </li></ol>
1 回答
![?](http://img1.sycdn.imooc.com/545865890001495702200220-100-100.jpg)
慕桂英3389331
TA贡献2036条经验 获得超8个赞
现有和新元素的解决方案:
$('body').on('click', 'input[type="checkbox"]', function() {
if ($(this).prop("checked") == true) {
alert("Checkbox is checked.");
} else if ($(this).prop("checked") == false) {
alert("Checkbox is unchecked.");
}
});
添加回答
举报
0/150
提交
取消