【问题标题】:If table checkbox is checked, uncheck other table checkboxes?如果选中表格复选框,取消选中其他表格复选框?
【发布时间】:2014-09-24 01:49:13
【问题描述】:

我有很多表格,当一个复选框被选中时 - 所有其他表格都应该清除。您可以选中此表中的任意数量的复选框,但一次只能选中一个表中的复选框。 我该怎么做呢?如果可能,我想避免使用 id,因为我有 8 个表。

http://jsfiddle.net/69o3e5y4/

$('input[type=checkbox]').on('click', function () {

});

【问题讨论】:

  • 听起来单选按钮非常适合这个。您是否有理由不想使用它们?
  • @microsby0: "您可以在此表中选中任意数量的复选框"
  • @DarkFalcon 啊谢谢,我没有注意到那部分。您可以偶然使用班级,每桌一个班级吗?它比 ids 好一点
  • 如何使用类/ID?如果这不是 table1、table2、table3、table4 等,我不想要一长串

标签: javascript jquery checkbox this checked


【解决方案1】:

你可以:

  1. 获取包含表$(this).closest("table")
  2. 然后获取所有其他表:$("table").not($table)
  3. 最后,取消选中这些表中的所有输入:... .prop("checked", false)

这应该给出一些这样的代码:

$('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
});

看到这个小提琴:http://jsfiddle.net/69o3e5y4/2/

【讨论】:

  • 此外,对于每个选中的复选框,我们可以记录 .closest('td').next().html() 作为名称吗?所以如果只有一个被选中,“red”,但如果超过一个添加一个逗号“red, blue”并且最后一个单词不应该有一个逗号“red, blue,”。
  • 知道了。 $('input[type=checkbox]').on('click', function () { var $table = $(this).closest("table"); $("table").not($table) .find("input[type=checkbox]:checked").prop("checked", false); var chklist = ""; $('input[type=checkbox]:checked').each(function () { var sThisVal = (this.checked ? "1" : "0"); chklist += (chklist=="" ? sThisVal : ", " + sThisVal); }); console.log (chklist); });
【解决方案2】:

此 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/

【讨论】:

    【解决方案3】:

    我还没有尝试过,但我会使用类。因此,您在 Table1 中的所有复选框都属于“Table1”类,Table2 中的所有复选框都属于“Table2”类,等等。

    然后,在您已经拥有的事件处理程序中,您可以遍历页面上的每个复选框,如果它们的类与被单击的类不匹配,则取消选中所有复选框。

    例如(同样,未测试):

            $("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);
                    });
                }
            });
    

    【讨论】:

    • -1 .prop('class')?如果复选框有多个类怎么办?
    • prop("class") 不返回列表 - 它返回一个字符串(空格分隔)。如果他的复选框都具有相同的 5 个类,他可以添加第六个(表名)并且此代码将起作用。如果复选框没有类,他可以添加一个(表名)并且此代码将起作用。
    • 我明白了。变量太多(if's),所以这听起来不是一个理想的解决方案。
    猜你喜欢
    • 2013-11-04
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多