Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用委托jquery函数获取选中复选框_Javascript_Jquery - Fatal编程技术网

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");
});