选择/取消选择所有复选框突出显示正常但检查错误,跨多个表
我有三个表,一个列出了联系人、其他用户和最后的电子邮件。用户可以单独选择人员,也可以单击全选复选框。当选择一个人时, 会突出显示,如果选中全选,则所有用户都会突出显示。到目前为止,一切都很好。
选中全选复选框时会出现问题。这将选择 中的所有
并突出显示它们。但是,它还会选择/取消选择其他两个表中的所有其他
。
总结,每个中的单独选择效果很好。全选/取消全选确实会检查表中的所有
并突出显示它们,但它也会检查其他表中的
,这就是我遇到的问题试图解决。
一种不错但不一定需要的方法是仅使用一个适合 3 个表的函数,而不是 3 个函数。
要了解整个情况: http://jsfiddle.net/moodas/C3dhm /8/
jQuery如下:
$(document).ready(function() {
// CONTACTS
$("#checkallContacts").live('click',function(event) {
$('input:checkbox:not(#checkallContacts)').attr('checked',this.checked);
//To Highlight
if ($(this).attr("checked") == true) {
$("#tblDisplayContacts").find('tr:not(#chkrowContacts)').css("background-color","#FC9A01"); }
else {
$("#tblDisplayContacts").find('tr:not(#chkrowContacts)').css("background-color","#FFF"); }
});
$('input:checkbox:not(#checkallContacts)').live('click',function(event) {
if($("#checkallContacts").attr('checked') == true && this.checked == false) {
$("#checkallContacts").attr('checked',false);
$(this).closest('tr').css("background-color","#ffffff"); }
if(this.checked == true) {
$(this).closest('tr').css("background-color","#FC9A01");
CheckSelectAll(); }
if(this.checked == false) {
$(this).closest('tr').css("background-color","#ffffff"); }
});
function CheckSelectAll() {
var flag = true;
$('input:checkbox:not(#checkallContacts)').each(function() {
if(this.checked == false) {
flag = false; }
});
$("#checkallContacts").attr('checked',flag);
}
// USERS
$("#checkallUsers").live('click',function(event) {
$('input:checkbox:not(#checkallUsers)').attr('checked',this.checked);
//To Highlight
if ($(this).attr("checked") == true) {
$("#tblDisplayUsers").find('tr:not(#chkrowUsers)').css("background-color","#FC9A01"); }
else {
$("#tblDisplayUsers").find('tr:not(#chkrowUsers)').css("background-color","#FFF"); }
});
$('input:checkbox:not(#checkallUsers)').live('click',function(event) {
if($("#checkallUsers").attr('checked') == true && this.checked == false) {
$("#checkallUsers").attr('checked',false);
$(this).closest('tr').css("background-color","#ffffff"); }
if(this.checked == true) {
$(this).closest('tr').css("background-color","#FC9A01");
CheckSelectAll(); }
if(this.checked == false) {
$(this).closest('tr').css("background-color","#ffffff"); }
});
function CheckSelectAll() {
var flag = true;
$('input:checkbox:not(#checkallUsers)').each(function() {
if(this.checked == false) {
flag = false; }
});
$("#checkallUsers").attr('checked',flag);
}
// EMAIL
$("#checkallEmail").live('click',function(event) {
$('input:checkbox:not(#checkallEmail)').attr('checked',this.checked);
//To Highlight
if ($(this).attr("checked") == true) {
$("#tblDisplayEmail").find('tr:not(#chkrowEmail)').css("background-color","#FC9A01"); }
else {
$("#tblDisplayEmail").find('tr:not(#chkrowEmail)').css("background-color","#FFF"); }
});
$('input:checkbox:not(#checkallEmail)').live('click',function(event) {
if($("#checkallEmail").attr('checked') == true && this.checked == false) {
$("#checkallEmail").attr('checked',false);
$(this).closest('tr').css("background-color","#ffffff"); }
if(this.checked == true) {
$(this).closest('tr').css("background-color","#FC9A01");
CheckSelectAll(); }
if(this.checked == false) {
$(this).closest('tr').css("background-color","#ffffff"); }
});
function CheckSelectAll() {
var flag = true;
$('input:checkbox:not(#checkallEmail)').each(function() {
if(this.checked == false) {
flag = false; }
});
$("#checkallEmail").attr('checked',flag);
}
});
I have three tables, one listing contacts, the other users, and the last emails. The user can either select people individually, or click on a select all checkbox. When a person is selected the <TR>
is highlighted, and if select all is check, then all users are highlighted. So far, so good.
The problem arises when checking the select all checkbox. This will select all <TR>
in the <TABLE>
and highlight them. However, it will also select/deselect all other <TR>
in the other two tables.
Summarizing, individual selections within each <TABLE>
work well. Select/deselect all does check all <TR>
in the table and highlights them, however it also checks <TR>
in other tables, which is the problem I am trying to solve.
A nice, but not necessarily need to have, would be to use only one function which suits the 3 tables rather than 3 functions.
To get the whole picture: http://jsfiddle.net/moodas/C3dhm/8/
jQuery below:
$(document).ready(function() {
// CONTACTS
$("#checkallContacts").live('click',function(event) {
$('input:checkbox:not(#checkallContacts)').attr('checked',this.checked);
//To Highlight
if ($(this).attr("checked") == true) {
$("#tblDisplayContacts").find('tr:not(#chkrowContacts)').css("background-color","#FC9A01"); }
else {
$("#tblDisplayContacts").find('tr:not(#chkrowContacts)').css("background-color","#FFF"); }
});
$('input:checkbox:not(#checkallContacts)').live('click',function(event) {
if($("#checkallContacts").attr('checked') == true && this.checked == false) {
$("#checkallContacts").attr('checked',false);
$(this).closest('tr').css("background-color","#ffffff"); }
if(this.checked == true) {
$(this).closest('tr').css("background-color","#FC9A01");
CheckSelectAll(); }
if(this.checked == false) {
$(this).closest('tr').css("background-color","#ffffff"); }
});
function CheckSelectAll() {
var flag = true;
$('input:checkbox:not(#checkallContacts)').each(function() {
if(this.checked == false) {
flag = false; }
});
$("#checkallContacts").attr('checked',flag);
}
// USERS
$("#checkallUsers").live('click',function(event) {
$('input:checkbox:not(#checkallUsers)').attr('checked',this.checked);
//To Highlight
if ($(this).attr("checked") == true) {
$("#tblDisplayUsers").find('tr:not(#chkrowUsers)').css("background-color","#FC9A01"); }
else {
$("#tblDisplayUsers").find('tr:not(#chkrowUsers)').css("background-color","#FFF"); }
});
$('input:checkbox:not(#checkallUsers)').live('click',function(event) {
if($("#checkallUsers").attr('checked') == true && this.checked == false) {
$("#checkallUsers").attr('checked',false);
$(this).closest('tr').css("background-color","#ffffff"); }
if(this.checked == true) {
$(this).closest('tr').css("background-color","#FC9A01");
CheckSelectAll(); }
if(this.checked == false) {
$(this).closest('tr').css("background-color","#ffffff"); }
});
function CheckSelectAll() {
var flag = true;
$('input:checkbox:not(#checkallUsers)').each(function() {
if(this.checked == false) {
flag = false; }
});
$("#checkallUsers").attr('checked',flag);
}
// EMAIL
$("#checkallEmail").live('click',function(event) {
$('input:checkbox:not(#checkallEmail)').attr('checked',this.checked);
//To Highlight
if ($(this).attr("checked") == true) {
$("#tblDisplayEmail").find('tr:not(#chkrowEmail)').css("background-color","#FC9A01"); }
else {
$("#tblDisplayEmail").find('tr:not(#chkrowEmail)').css("background-color","#FFF"); }
});
$('input:checkbox:not(#checkallEmail)').live('click',function(event) {
if($("#checkallEmail").attr('checked') == true && this.checked == false) {
$("#checkallEmail").attr('checked',false);
$(this).closest('tr').css("background-color","#ffffff"); }
if(this.checked == true) {
$(this).closest('tr').css("background-color","#FC9A01");
CheckSelectAll(); }
if(this.checked == false) {
$(this).closest('tr').css("background-color","#ffffff"); }
});
function CheckSelectAll() {
var flag = true;
$('input:checkbox:not(#checkallEmail)').each(function() {
if(this.checked == false) {
flag = false; }
});
$("#checkallEmail").attr('checked',flag);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以这样做:
旁注,您经常使用 live,所有这些表都是动态构建的吗?
另外看看你的例子,我认为这可以简化为以下内容,这依赖于你的表的类名约定,并消除了对 id 的需要:
这是 jsfiddle。
You could do something like this:
On a side note, you are using live quite a bit, are all of these tables built dynamically?
Also looking at your example I think this can be simplified to the following this relies on a convention for your tables by class name and removes the need for an id:
Here is an example of this on jsfiddle.