简体   繁体   English

复选框处于活动状态时,jQuery禁用其他行中的输入

[英]jQuery disable input in other row when checkbox is active

I want to enable/disable input when checkbox is checked. 我想在选中复选框时启用/禁用输入。 I have many sections in my site with the same structure so I know I need to use 我的网站中有很多部分的结构相同,所以我知道我需要使用

this

construction. 施工。 I tried to do something like this but it doesn't work. 我试图做这样的事情,但是没有用。 Anyone could help me? 有人可以帮助我吗?

 $('.component-other').on('click', function(){ var isChecked = $(this).is(':checked'); if (isChecked) { $(this).closest('.row').find('.component-other-value').prop("disabled", false); } else { $(this).closest('.row').find('.component-other-value').prop("disabled", true); } }); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <section> <div class="container"> <div class="row"> <div class="col-sm-12"> <label for="sause-oth">Other</label> <input type="checkbox" name="sause" id="sause-oth" class="enable component-other"> </div> </div> <div class="row input-row"> <div class="col-sm-12"> <input type="text" name="sause-other" id="sause-other" class="component-other-value" disabled="disabled" placeholder="Type text here"> </div> </div> </div> </section> <section> <div class="container"> <div class="row"> <div class="col-sm-12"> <label for="sause-oth">Other</label> <input type="checkbox" name="sause" id="sause-oth-1" class="enable component-other"> </div> </div> <div class="row input-row"> <div class="col-sm-12"> <input type="text" name="sause-other" id="sause-other-1" class="component-other-value" disabled="disabled" placeholder="Type text here"> </div> </div> </div> </section> 

The closest .row is not enough. 最接近的.row是不够的。 You have to step one more layer up the DOM. 您必须再上一层DOM。

 $('.component-other').on('click', function(){ $(this).closest('.container') .find('.component-other-value') .prop("disabled", !this.checked); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <section> <div class="container"> <div class="row"> <div class="col-sm-12"> <label for="sause-oth">Other</label> <input type="checkbox" name="sause" id="sause-oth" class="enable component-other"> </div> </div> <div class="row input-row"> <div class="col-sm-12"> <input type="text" name="sause-other" id="sause-other" class="component-other-value" disabled="disabled" placeholder="Type text here"> </div> </div> </div> </section> <section> <div class="container"> <div class="row"> <div class="col-sm-12"> <label for="sause-oth">Other</label> <input type="checkbox" name="sause" id="sause-oth-1" class="enable component-other"> </div> </div> <div class="row input-row"> <div class="col-sm-12"> <input type="text" name="sause-other" id="sause-other-1" class="component-other-value" disabled="disabled" placeholder="Type text here"> </div> </div> </div> </section> 

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

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