Javascript 如何使用委托jquery函数获取选中复选框
我已在表中动态创建了复选框列表:Javascript 如何使用委托jquery函数获取选中复选框,javascript,jquery,Javascript,Jquery,我已在表中动态创建了复选框列表: $("#employeeRegister").append('<tr><td><input type="checkbox" class = "check" name ="chk'+i+'" value="'+this.employeeMobileNo+'$'+this.employeeEmailId+'" </td></tr>'); smsbutton是一个按钮,我希望在其单击事件上获得选中的复选框。但它不
$("#employeeRegister").append('<tr><td><input type="checkbox" class = "check" name ="chk'+i+'" value="'+this.employeeMobileNo+'$'+this.employeeEmailId+'" </td></tr>');
smsbutton
是一个按钮,我希望在其单击事件上获得选中的复选框。但它不起作用。如何才能选中所有复选框?只需使用属性选择器,如
$(document).on("click","#smsbutton", function(){
$('input[type="checkbox"]:checked');
console.log($('input[type="checkbox"]:checked').serialize());
});
或
试试这个:
$("input.check:checked").each(function() {
alert(this.name + " is checked");
});
您的选择器是
[name=type]
,但是append
复选框是name='chk'+i
取决于我的数据库中的行数。checked')。每个(函数()
运行的次数与没有得到值的复选框的次数相同。您将始终需要循环通过。好的,现在这不会打印两次。我编辑了它。这对您有用吗?
$(document).on("click","#smsbutton", function(e){
if (e.handled !== true) {
e.handled = true;
e.preventDefault();
$('input[type="checkbox"]:checked').each(function() {
console.log(this.value);
});
}
});
$("input.check:checked").each(function() {
alert(this.name + " is checked");
});