javascript - 当复选框处于事件状态时,jQuery 禁用其他行中的输入

标签 javascript jquery html

我想在选中复选框时启用/禁用输入。我的网站中有许多具有相同结构的部分,所以我知道我需要使用

this

build 。我尝试做这样的事情但它不起作用。有人可以帮助我吗?

$('.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>

最佳答案

最近的 .row 是不够的。您必须在 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>

关于javascript - 当复选框处于事件状态时,jQuery 禁用其他行中的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48042748/

相关文章:

html - Silverlight 小程序(<object> 标签)不在其包含的 <div> 标签内

html - 在背景图像之前/之后使用 css 中的数据属性

javascript - 使用 `innerHTML` 是在具有 `button` 属性的 JavaScript 中创建 `type` 元素的唯一方法吗?

javascript - css定位难度

javascript - 在没有 disconnectedCallback 的情况下将元素从 light DOM 移动到 ShadowDOM

javascript - 在 IE 7 中,如果链接是使用 jQuery 创建的,您如何获取链接的 href 属性的字面值?

javascript - 自动完成仅在页面加载或输入一个字母时运行一次,但仅运行一次

javascript - Ionic 2 Modal 不会关闭

javascript - 如何替换 Rails Controller 中的内联 JavaScript

javascript - 在 node.js 中获取调用函数的名称和行