使用 jquery 禁用表中的所有复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1426419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:26:00  来源:igfitidea点击:

Disable all check boxes inside a table with jquery

jquerycheckbox

提问by user152248

I need to disable all the check boxes inside a table cell when clicking on a hyperlink inside the same table.

单击同一表格内的超链接时,我需要禁用表格单元格内的所有复选框。

I'm using the following jquery code to select all the check boxes nested inside the table.

我正在使用以下 jquery 代码来选择嵌套在表中的所有复选框。

$el = $(this).parents('table:eq(0)')[0].children('input[type="checkbox"]');
$($el).attr('checked', true);

For some reason this piece of code is not working.

由于某种原因,这段代码不起作用。

Can anyone show me how to fix it?

谁能告诉我如何解决它?

回答by Tzury Bar Yochay

$('table input[type=checkbox]').attr('disabled','true');

if you have an id for the table

如果你有表的 id

$('table#ID input[type=checkbox]').attr('disabled','true');

回答by Sampson

Disable?

禁用?

$("a.clickme").click(function(){
  $(this)                    // Link has been clicked
    .closest("td")           // Get Parent TD
    .find("input:checkbox")  // Find all checkboxes
    .attr("disabled", true); // Disable them
});

or Checked?

或检查?

$("a.clickme").click(function(){
  $(this)                    // Link has been clicked
    .closest("td")           // Get Parent TD
    .find("input:checkbox")  // Find all checkboxes
    .attr("checked", false); // Uncheck them
});

回答by Philippe Leybaert

Your code can be a lot simpler:

您的代码可以简单得多:

$el = $(this).parents('table:eq(0)')[0].children('input[type="checkbox"]');

Could be:

可能:

$el = $(this).parents('table:first :checkbox');

Then to disable them:

然后禁用它们:

$el.attr('disabled', 'disabled');

or to check them:

或检查它们:

$el.attr('checked', 'checked');

or to uncheck them:

或取消选中它们:

$el.removeAttr('checked');

or to enable them:

或启用它们:

$el.removeAttr('disabled');

回答by ranonE

See also: selector/checkbox

另见:选择器/复选框

jQuery("#hyperlink").click(function() {
  jQuery('#table input:checkbox').attr('disabled', true);
  return false;
});

回答by Brynner Ferreira

// Enable/Disable All Checkboxes

// 启用/禁用所有复选框

$('#checkbox').click(function() {

    var checked = $(this).attr('checked');
    var checkboxes = '.checkboxes input[type=checkbox]';

    if (checked) {
        $(this).attr('checked','checked');
        $(checkboxes).attr('disabled','true');
    } else {
        $(this).removeAttr('checked');
        $(checkboxes).removeAttr('disabled');
    }
});

回答by m4dd

This is my solution

这是我的解决方案

// Action sur le checkbox
        $("#tabEmployes thead tr th:first input:checkbox").click(function() {
            var checked = $(this).prop('checked');
            $("#tabEmployes tbody tr td:first-child input:checkbox").each(function() {
                $(this).prop('checked',checked);
            });
        });

回答by Raja Danish

------------------------------- HTML Code below ------------------------------

------------------------------- 下面的 HTML 代码 ---------------- --------------

<table id="myTable">
    <tr>
        <td><input type="checkbox" checked="checked" /></td>
        <td><input type="checkbox" checked="checked" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
    </tr>
</table>

<input type="button" onclick="callFunction()" value="Click" />

------------------------------- JQuery Code below -----------------------------

------------------------------- JQuery 代码如下 ---------------- -------------

    <script type="text/javascript">
    function callFunction() {
        //:
        $('table input[type=checkbox]').attr('disabled', 'true');
    }
</script>