如何将所有复选框的id传递给javascript函数

How to pass the ids of all the checkboxes to a javascript function

本文关键字:javascript 函数 id 复选框      更新时间:2023-09-26

我使用Jquery在一个复选框的帮助下启用表中的所有复选框。现在,我需要将所有选中复选框的id传递给一个函数(selected_checkbox(id))。如果我手动选中/取消选中表中的复选框,我可以将该id传递给所需的函数。但是,当我选择"chkCheckAll"复选框来选择表中存在的所有复选框(实际上使用JQuery)时,我无法将所有其他复选框的ID传递给javascript函数。请帮助

HTML内容可在http://ideone.com/90kiu(我对stackoverflow比较陌生,所以我不知道如何将代码放在这个区域)

您可以对每个创建id数组的对象进行迭代,例如:

var ids = [];
$("form input:checkbox").each(function() {
    //check the checkbox
    $(this).prop("checked", true);;
    //add the id to the array
    ids.push(this.id);
});
//check all the checkboxes and return an array of id's
var ids = $("form input:checkbox").prop("checked", true).id();

获取所有复选框ID:

var ids= $('input[type="checkbox"]').id();

或者jquery有一个选中的选择器:

var ids = $('input:checked').id();

如果你只是想检查一下。

$('input:checked').serialize();

您也可以使用

document.querySelectorAll('[type=checkbox]');   

它将以数组的形式返回所有复选框。你可以使用它们做更多的事情。