简体   繁体   English

选中复选框时禁用选择

[英]Disabling a Select When a CheckBox is Checked

I am working on a large product and for the life of me I cannot get this to work. 我正在开发一个大型产品,为了我的一生,我无法使它正常工作。 My problem, is that the id of the checkboxes and the drop-down boxes are dynamic so I simply cannot reference them and I cannot edit the html to give them another. 我的问题是,复选框和下拉框的ID是动态的,因此我无法引用它们,也无法编辑html以给它们另一个。 I can however, contain them in a Div and give that a class name to reference them (div.blaaah select). 但是,我可以将它们包含在Div中,并给该类一个名称以引用它们(div.blaaah select)。

I have been trying to research and write some JS to disable the drop-down boxes when a checkbox is selected but with the referencing issues I cannot do it. 我一直在尝试研究和编写一些JS,以便在选中复选框时禁用下拉框,但是由于存在引用问题,我无法做到这一点。

Also I want this to apply many many areas on the page (around 65 checkboxes disable corresponding pairs of drop-down boxes) - for example: 我也希望它在页面上应用很多区域(大约65个复选框禁用相应的下拉框对)-例如:

例

as each div containg a drop-down box is the next element after the div containing the checkbox to disable it I thought I could use .next() or something but my skills with Js just arent up to scratch! 因为每个div包含一个下拉框是div之后包含禁用复选框的div的下一个元素,我以为我可以使用.next()或其他东西,但是我的Js技能简直太划算了!

I have attempted a host of things but the generel idea I am going for is along the lines of the following shambles of an attempt: 我已经尝试了很多事情,但是我要追求的宽泛的想法是遵循以下尝试的思路:

$(".NACheck input").click(function() {
$(this).next(".importSelect select").disabled=true;
});

This is the heirachy of the elements: 这是元素的异常:

<td>
  <div>
    <div class="checkNA">
      <table>
        <tbody>
          <tr>
            <td>
              <span>
                <table>
                  <tbody>
                    <tr>
                      <td>
                        <INPUT: CHECKBOX />
                      </td>
                      <td>
                      </td>
                    </tr>
                  </tbody>
                </table>
              </span>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</td>
<td>
  <div>
    <div class="importSelect">
      <table>
        <tbody>
          <tr>
            <td>
              <span>
                <SELECT />
              </span>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</td>
<td>
  <div>
    <div class="RelSelect">
      <table>
        <tbody>
          <tr>
            <td>
              <span>
                <SELECT />
              <span>
            </td>
          </tr>
        <tbody>
      </table>
    </div>
  </div>
</td>    

Any help would be much appreciated! 任何帮助将非常感激! Thank you. 谢谢。

Try this : 尝试这个 :

$(".NACheck input").click(function() {
    $('select', $(this).parents('.container-of-each-line')).attr('disabled', 'disabled');
});

Assuming your select boxs are next two elements after checkbox 假设您的select boxs checkbox之后的next two elements

add onchange or onclick event to each checkbox as onchangeonclick事件添加到每个复选框,如下所示:

this.nextSibling.disabled = this.checked;
this.nextSibling.nextSibling.disabled = this.checked;

May be this can help: 可能可以这样:

$(this).parents('div containing checkbox').next().children().attr("disabled", true);

check this out http://jsfiddle.net/ZBymu/ 看看这个http://jsfiddle.net/ZBymu/

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

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