javascript - 更改选择框时启用文本区域

标签 javascript jquery

我有选择框数组和文本区域数组,具有相同的id

  <div class="reasons">
       <select id="reason" class="reason_select" name="reason[]">
           <option>Please Select</option>
           <option value="misc">Misc</option>
        </select>
        <textarea id="reason_comment" name="reason_comment[]" disabled="disabled/>

        <select id="reason" class="reason_select" name="reason[]">
           <option>Please Select</option>
           <option value="misc">Misc</option>
        </select>
        <textarea id="reason_comment" name="reason_comment[]" disabled="disabled/>

        <select id="reason" class="reason_select" name="reason[]">
           <option>Please Select</option>
           <option value="misc">Misc</option>
        </select>
        <textarea id="reason_comment" name="reason_comment[]" disabled="disabled/>
  </div>

在这三对中,更改任何选择框时我需要启用相应的文本区域。

$(document).on("change", ".reason_select", function(){
    //not getting where to start and how to identify corresponding textarea.
});

最佳答案

HTML 中的标识符必须是唯一的 好读 Why is it a bad thing to have multiple HTML elements with the same id attribute?

您可以使用$.fn.next()遍历到直接兄弟。

使用

$(document).on("change", ".reason_select", function(){
    $(this).next('textarea').prop('disabled', false)
});

关于javascript - 更改选择框时启用文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30624543/

相关文章:

javascript - 将 Prop 从 child 传递给 parent 不起作用(React)

javascript - preventDefault 在提交按钮中不起作用

javascript - 将 &lt;input&gt; 更改为 <img> 标签

javascript - dropzone.js: __slice = [].slice

javascript - datetimepicker 没有出现在 Bootstrap 对话框中

jquery - fullpage.js 如何创建幻灯片 anchor 并滚动到它们?

jquery - 如何添加图像上传以及ajax jquery表单提交

javascript - sweetalert 立即消失

javascript - 使用JS在url中插入rel=0

javascript - 在 Angular 2+ 上重新渲染 FullCalendar