【问题标题】:Disable checkboxes that contain different attribute禁用包含不同属性的复选框
【发布时间】:2014-10-01 00:21:06
【问题描述】:

一旦选择了仓库,我就会尝试禁用具有不同仓库位置的复选框。例如,如果我选中加利福尼亚,我想禁用所有来自其他州的复选框。我试图根据属性 whseID 来做,但我不知道如何让 jquery 区分该属性。有时我会从 1 个仓库运送多件物品。因此,当我选中加利福尼亚的 1 个复选框时,我需要加利福尼亚的另一个复选框保持启用状态,但我需要禁用华盛顿和亚利桑那。

<table width="50%" border="0" cellspacing="0" cellpadding="0">
    <tr class="grdnt">
        <th style="color:black;">Checkbox</th>
        <th style="color:black;border-left:1px solid;">Warehouse</th>
        <th style="color:black;border-left:1px solid;">Item</th>
    </tr>
    <tr id="transferDetailCol">
        <td>
            <input type="checkbox" class="tfrCheck" name="tfrCheck[]" whseID="CA" />
        </td>
        <td class="Whse">California</td>
        <td class="Item">J29458</td>
    </tr>
    <tr id="transferDetailCol">
        <td>
            <input type="checkbox" class="tfrCheck" name="tfrCheck[]" whseID="CA" />
        </td>
        <td class="Whse">California</td>
        <td class="Item">J29478</td>
    </tr>
    <tr id="transferDetailCol">
        <td>
            <input type="checkbox" class="tfrCheck" name="tfrCheck[]" whseID="WA" />
        </td>
        <td class="Whse">Washington</td>
        <td class="Item">J29478</td>
    </tr>
    <tr id="transferDetailCol">
        <td>
            <input type="checkbox" class="tfrCheck" name="tfrCheck[]" whseID="AZ" />
        </td>
        <td class="Whse">Arizona</td>
        <td class="Item">J29478</td>
    </tr>
</table>

$(document).on('click', '.tfrCheck', function () {
    var allCheckBox = $(".tfrCheck");
    var count_checked = allCheckBox.filter(":checked").length;
    var whseID = $(this).attr('whseID');
    if (count_checked >= 1) {

        $(".tfrCheck:contains(whseID)").attr('disabled', 'disabled');
        //$(".tfrCheck:not(:checked)").attr('disabled','disabled');

    } else {
        $(".tfrCheck").removeAttr('disabled');
    }
});

JSFIDDLE

【问题讨论】:

    标签: javascript jquery checkbox


    【解决方案1】:

    Ragnar 的答案很接近,但由于您希望禁用其他状态,请使用

    $('.tfrCheck:not([whseID='+ whseID +'])').attr('disabled', 'disabled');
    

    【讨论】:

      【解决方案2】:

      尝试使用这一行:

      $('.tfrCheck:not([whseID='+ whseID +'])').attr('disabled', 'disabled');
      

      【讨论】:

      • 我相信= 应该是!=。
      【解决方案3】:

      “属性不等于选择器”应该会有所帮助:

      allCheckBox.filter('[whseID!="' + whseID + '"]').attr('disabled', true);
      

      http://jsfiddle.net/oxkeL7jm/4/

      【讨论】:

        猜你喜欢
        • 2014-01-23
        • 2022-10-31
        • 1970-01-01
        • 2016-02-14
        • 1970-01-01
        • 1970-01-01
        • 2022-07-06
        • 1970-01-01
        相关资源
        最近更新 更多