如果选中表格复选框,请取消选中其他表格复选框?

tri*_*t77 0 javascript checkbox jquery this checked

我有很多表,当一个复选框被选中时 - 所有其他表应该清除.您可以在此表中检查任意数量的复选框,但只能一次检查一个表中的复选框.我该怎么做呢?我希望尽可能避免使用id,因为我有8个表.

http://jsfiddle.net/69o3e5y4/

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

});
Run Code Online (Sandbox Code Playgroud)

Sam*_*rie 5

您可以 :

  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
});
Run Code Online (Sandbox Code Playgroud)

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