简体   繁体   English

如果选中了表格复选框,则取消选中其他表格复选框吗?

[英]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 : 您可以 :

  1. get the containing table $(this).closest("table") 获取包含表$(this).closest("table")
  2. then get all other tables : $("table").not($table) 然后获取所有其他表: $("table").not($table)
  3. finally, uncheck all inputs from these tables : ... .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.

相关问题 取消选中复选框将取消选中表中的其他复选框 - Unchecking a Checkbox will uncheck other checkboxes in a table 如果选中了“其他”复选框,则取消选中所有复选框并获取价值 - Uncheck all checkboxes if “Other” checkbox is checked and get value 当组中的一个复选框被选中时,禁用并取消选中所有其他复选框 - When one checkbox is checked in a group, disable & uncheck all other checkboxes 复选框onclick取消选中其他复选框 - Checkbox onclick uncheck other checkboxes 仅显示选中复选框的值,如果选中其他复选框,则取消选中它们 - Display value from only checked checkbox and if other checkboxes are checked uncheck them 如果选中的复选框超过3个,如何取消选中该复选框 - How to uncheck a checkbox if there are checked more than 3 checkboxes 如果勾选了一个复选框,如何取消选中所有复选框? - How to uncheck all checkboxes, if one checkbox checked? 单击复选框后,取消选中其他复选框并触发更改或单击先前选中的复选框的事件 - Upon clicking a checkbox, uncheck other checkboxes and trigger change or click event on the on one that was previously checked 选中/取消选中表格中的所有复选框 - Check/Uncheck all the checkboxes in a table 选中复选框后,取消选中页面上的其他复选框 - Uncheck other checkboxes on page when checking a checkbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM