简体   繁体   English

如何在JQuery中单击最近元素(Checkbox)时禁用下一个元素(textarea)?

[英]How to disable the next element(textarea) on click of nearest element(Checkbox) in JQuery?

On click of Check box, i want the next nearest Text area to get disabled. 单击复选框,我希望下一个最近的文本区域被禁用。

Following is snippet: 以下是片段:

<div class="pure-u-1 pure-u-md-1-5">
  <input type="checkbox" value="true" class="fRegistrer" name="fRegistrer" id="fRegistrer">
</div>

<div class="pure-u-1 pure-u-md-1-5">
  <textarea cols="200" rows="5" class="normal" name="kommenter" id="kommenter"></textarea>
</div>

in Jquery: 在Jquery:

 onclick="$(this).closest('.textnormal').find('textarea').attr('disabled',true)"

On click of the checkbox you can use closest() to get the parent div , next() to find the next sibling div , then find() to get the contained textarea . 单击复选框,您可以使用closest()获取父divnext()查找下一个兄弟div ,然后使用find()获取包含的textarea Try this: 尝试这个:

 $(':checkbox').change(function() { $(this).closest('div').next('div').find('textarea').prop('disabled', !this.checked); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="pure-u-1 pure-u-md-1-5"> <input type="checkbox" value="true" class="fRegistrer" name="fRegistrer" id="fRegistrer"> </div> <div class="pure-u-1 pure-u-md-1-5"> <textarea cols="200" rows="5" class="normal" name="kommenter" id="kommenter" disabled="true">Foo bar</textarea> </div> 

$('#fRegistrer').click(function () {
    debugger
    $(this).parent('.pure-u-1').next().find('#kommenter').attr('disabled',true);
});  

DEMO DEMO

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

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