[英]If table checkbox is checked, uncheck other table checkboxes?
I have numerous tables, and when a checkbox is checked in one - all other tables should clear. 我有很多表格,并且选中一个复选框时,所有其他表格都应清除。 You can check any number of checkboxes in this table, but only able to check checkboxes in one table at a time. 您可以选中此表中的任意数量的复选框,但一次只能选中一个表中的复选框。 How would I go about doing this? 我将如何去做呢? I want to avoid using ids if possible because I have 8 tables. 我想避免使用ID,因为我有8个表。
http://jsfiddle.net/69o3e5y4/ http://jsfiddle.net/69o3e5y4/
$('input[type=checkbox]').on('click', function () {
});
You can : 您可以 :
$(this).closest("table")
获取包含表$(this).closest("table")
$("table").not($table)
然后获取所有其他表: $("table").not($table)
... .prop("checked", false)
. 最后,取消选中这些表中的所有输入: ... .prop("checked", false)
。 This should give some code like this : 这应该给出一些这样的代码:
$('input[type=checkbox]').on('click', function () {
var $table = $(this).closest("table"); // Current table
$("table").not($table) // Other tables
.find("input[type=checkbox]:checked") // Checked input of these tables
.prop("checked", false); // Uncheck them
});
See this fiddle : http://jsfiddle.net/69o3e5y4/2/ 看到这个小提琴: http : //jsfiddle.net/69o3e5y4/2/
This jquery works without any ids: 这个jQuery的工作没有任何ID:
$('table').each(function(index){
$(this).find('input').each(function()
{
$(this).on('click', function () {
clearThisTablesCheckboxes(index);
});
});
});
function clearThisTablesCheckboxes(position){
$('table').each(function(index){
$(this).find('input').each(function()
{
if(position != index)
{
var prop=false;
$(this).prop('checked',prop);
}
});
});
}
http://jsfiddle.net/csdtesting/69o3e5y4/8/ http://jsfiddle.net/csdtesting/69o3e5y4/8/
I haven't tried this, but I would use classes. 我没有尝试过,但是我会使用类。 So all your checkboxes in Table1 would be class "Table1", all checkboxes in Table2 would be in class "Table2", etc. 因此,表1中的所有复选框均为“表1”类,表2中的所有复选框均在“表2”类中,依此类推。
Then, within the event handler you already have, you can iterate through each checkbox on the page, and uncheck all of them if their class doesn't match the one that was clicked. 然后,在您已经拥有的事件处理程序中,您可以遍历页面上的每个复选框,如果它们的类与单击的类不匹配,则取消选中所有它们。
For example (again, not tested): 例如(再次,未经测试):
$("input[type=checkbox]").click(function () {
if ($(this).prop("checked") == true){
var CurrentClass = $(this).prop("class");
$("input[type=checkbox]").each(function () {
if ($(this).prop("class") != CurrentClass)
$(this).prop("checked", false);
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.