简体   繁体   English

根据值重复选择复选框

[英]Repeated selecting of checkboxs based on value

Using Jquery I have code that works to select all the checkboxes of the same value as the checkboxes that is selected. 使用Jquery,我的代码可以选择与选定复选框具有相同值的所有复选框。 It also disables all checkboxes on the same row as the checkbox that is selected (the checkboxes are within a table). 它还禁用与所选复选框在同一行的所有复选框(这些复选框在表中)。

This is all great. 太好了 However it is slightly incomplete, and the last piece of the puzzle is eluding me. 但是,这还有些残缺,难题的最后一步使我难以理解。 Checkboxes on the rows that are disabled are, by definition, disabled. 根据定义,禁用的行上的复选框是禁用的。 However, it would be perfect if all checkboxes with the same value as those disabled were also disabled. 但是,如果也禁用所有具有与禁用值相同的值的复选框,那将是完美的。 This is to prevent users from selecting incompatible items (there's also a server side measure to prevent this, but client side is so much more pretty and user-friendly). 这是为了防止用户选择不兼容的项(还有服务器端的措施可以防止这种情况发生,但是客户端要漂亮得多并且对用户友好)。

JSFiddle: https://jsfiddle.net/q9q7h1y8/ JSFiddle: https ://jsfiddle.net/q9q7h1y8/

$(".chkbox").on('change', function() {

    var currentObj = $(this);
    var val = $(this).val();
    if ($(this).is(":checked")) {

        $(":checkbox[value='" + val + "']").prop("checked", true);
        $(this)
            .closest("tr")
            .find(".chkbox").attr("disabled", "disabled");
        $(currentObj).removeAttr("disabled");
        $(":checkbox[value='" + val + "']")
            .closest("tr")
            .find(".chkbox").attr("disabled", "disabled");
        $(":checkbox[value='" + val + "']").removeAttr("disabled");
    } else {
        $(":checkbox[value='" + val + "']").prop("checked", false);
        $(":checkbox[value='" + val + "']")
            .closest("tr")
            .find(".chkbox").removeAttr('disabled');;

    }

});

The intended functionality would, for instance, disable both checkboxes related to 30680 on the jsfiddle link if any checkbox related to 20040 is checked. 例如,如果选中了与20040相关的任何复选框,则预期的功能将在jsfiddle链接上禁用与30680相关的两个复选框。

$(":checkbox[value='" + val + "']")
        .closest("tr")
        .find(".chkbox").attr("disabled", "disabled");

The above code will find all checkboxes on the same row and disable all of them. 上面的代码将在同一行中找到所有复选框,并禁用所有复选框。 I recommend looping through these checkboxes, disabling them and in addition, selecting all other checkboxes having the same values and disabling them. 我建议循环浏览这些复选框,将其禁用,此外,选择所有其他具有相同值的复选框并将其禁用。 You can do so by replacing the above code with the one below: 您可以通过将以下代码替换为以下代码来实现:

$(":checkbox[value='" + val + "']")
  .closest("tr").find(".chkbox").each(function(){
    var $this = $(this);
    $this.attr('disabled', 'disabled');
    $(":checkbox[value='" + $this.val() + "']").attr('disabled', 'disabled');
});

You will have to a similar thing for removing the the disabled attribute. 您将必须执行类似的操作才能删除Disabled属性。

$(":checkbox[value='" + val + "']")
  .closest("tr").find(".chkbox").each(function(){
    var $this = $(this);
    $this.removeAttr('disabled');
    $(":checkbox[value='" + $this.val() + "']").removeAttr('disabled');
});

Demo 演示版

Check updated fiddle here jsfiddle.net/bharatsing/q9q7h1y8/2/ 在这里检查更新的小提琴jsfiddle.net/bharatsing/q9q7h1y8/2/

Added some code in your fiddle code: 在您的小提琴代码中添加了一些代码:

$(":checkbox[value='" + val + "']")
                             .closest("tr")
                             .find(".chkbox").each(function(){
                                $(":checkbox[value='" + $(this).val() + "']").attr("disabled", "disabled");
                             });

每次更改复选框时,都可以存储其值,然后执行以下操作:

$('* :input[value="value?"]').action();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM