Javascript 如何获取以逗号分隔的选中复选框id,其值为1
我已选中“选择所有脚本”复选框Javascript 如何获取以逗号分隔的选中复选框id,其值为1,javascript,jquery,html,Javascript,Jquery,Html,我已选中“选择所有脚本”复选框 $(document.body).on('click', '#selecctall', function () { if (this.checked) { // Check select status $('.chkAssets').each(function () { // Loop through each checkbox this.checked = true; // Select all checkboxe
$(document.body).on('click', '#selecctall', function () {
if (this.checked) { // Check select status
$('.chkAssets').each(function () { // Loop through each checkbox
this.checked = true; // Select all checkboxes with class "chkAssets"
$(this).attr('value', this.checked ? 1 : '')
});
} else {
$('.chkAssets').each(function () { // Loop through each checkbox
this.checked = false; // Deselect all checkboxes with class "chkAssets"
$(this).attr('value', this.checked ? '' : '')
});
}
});
现在,我想选中复选框id
,并用逗号分隔,其值为1
注意:我想获得按钮点击事件的结果。
我的JSFIDLE工作:试试看
$("#getAll").click(function () {
var id = $("[type=checkbox][value=1]:checked").map(function () {
return this.id;
}).get().join(",");
alert(id);
});
我更新了您的代码
那么,将值设置为
1
的目的是什么?若要指示已使用“全选”复选框选择了元素,请执行以下操作:谢谢您的支持。
var selected = new Array();
$('input:checked').each(function() {
selected.push($(this).attr('id'));
})
$(document.body).on('click', '#selecctall', function () {
$("input:checkbox").attr("checked", $(this).is(":checked"));
});
$("#getAll").click(function () {
var ids = "";
$("input[type='checkbox']:checked").not("#selecctall").each(function () {
ids = ids + $(this).attr("id") + ",";
})
alert(ids);
})