Javascript 单击特定复选框时,是否选中表中的所有复选框?
我以这种方式使用tr和td动态地向表中添加复选框Javascript 单击特定复选框时,是否选中表中的所有复选框?,javascript,jquery,html,checkbox,html-table,Javascript,Jquery,Html,Checkbox,Html Table,我以这种方式使用tr和td动态地向表中添加复选框 var arr=[1,2,3]; var trtest = "<tr>"; trtest = trtest +"<tr><td><input type='checkbox' onclick=onAllCheckBoxClick('" + arr+ "') class = 'all' id ='all'>All</td>&l
var arr=[1,2,3];
var trtest = "<tr>";
trtest = trtest +"<tr><td><input type='checkbox' onclick=onAllCheckBoxClick('" + arr+ "') class = 'all' id ='all'>All</td></tr>";
$.each(arr, function(i, tmp) {
trtest = trtest +"<tr><td><input type='checkbox' onclick=onCheckBoxClick('" + this+ "','" + arr+ "') class = 'all' id ='"+tmp+"'/>"+tmp+"</td></tr>";
});
在这里,当我尝试选中所有复选框时,必须选中同一表1、2、3中的所有其他复选框
我尝试将此单击事件添加到所有复选框
function onAllCheckBoxClick(arr){
var checked = $('#all').attr('checked');
if(checked =="checked") {
$.each(arr.split(","), function(i, tmp) {
$('#'+tmp).attr('checked',true);
});
}
}
问题是当我全部选中时,它只选中3,即同一表格中的最后一个复选框,其中1,2保持未选中状态
但我希望在选中所有复选框时选中所有1,2,3复选框
function onAllCheckBoxClick(arr){
var checked = $('#all').attr('checked');
if(checked =="checked") {
$.each(arr.split(","), function(i, tmp) {
$('#'+tmp).attr('checked',true);
});
}
}
有人能帮我解决这个问题吗?试试这个:
$function{
$master.onclick,functionevent{
$:checkbox.attr'checked',true;
};
};
只是另一种方式
$('#all').toggle(
function() {
$(':checkbox').attr('checked','checked');
},
function() {
$(':checkbox').removeAttr('checked');
}
);
$document.readyfunction{
$'mainChkBox'.changefunction{
$':复选框'.prop'checked',this.checked;
};
};
点击这里
$'input:checkbox'.prop'checked',true您可以在此处检查动态添加的复选框是如何被选中的