javascript - 如何在 jquery 中隐藏(或显示)子下拉列表(动态 html)?

标签 javascript jquery html css toggle

这里创建一个动态表格行,当点击 + 按钮添加一个新行并点击 - 按钮删除行设计时,像这样,

enter image description here

Here subject drop-down display and also instructor drop-down display but the problem is when select subject maths instructor drop-down show and when select a science instructor drop-down hide but it's changing in all drop-down.

$('body').on('change', '.course_topic', function() {
  var topic_name = $(this).val();
  var names = ['Registration', 'Lunch Break', 'Tea Break'];

  if (jQuery.inArray(topic_name, names) != '-1') {
    $(this).closest('table').find('tbody#schedule_table').find('td:last').parent().find('td').hide();
  } else {
    $(this).closest('table').find('tbody#schedule_table').find('td:last').parent('td').find('td').show();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
  <tbody>
    <tr>
      <th>From Time</th>
      <th>To Time</th>
      <th>Subject</th>
      <th>Instructor</th>
      <th></th>
    </tr>
  </tbody>
  <tbody id="schedule_table">
    <tr id="ScheduleTable1">
      <td><input name="data[CourseSchedule][schedule_from_time][]" class="form-control from_timepicker" readonly="" value="11:54 AM" type="text" id="CourseScheduleScheduleFromTime"></td>
      <td><input name="data[CourseSchedule][schedule_to_time][]" class="form-control to_timepicker" readonly="readonly" value="01:54 AM" type="text" id="CourseScheduleScheduleToTime"></td>
      <td>
        <select name="data[CourseSchedule] 
               [schedule_subject][]" default="" class="form-control select2me 
               course_topic" id="CourseScheduleScheduleSubject">
          <option value="">Select Subject</option>
          <option value="gfgfg" selected="selected">gfgfg</option>
          <option value="Registration">Registration</option>
          <option value="Lunch Break">Lunch Break</option>
          <option value="Tea Break">Tea Break</option>
        </select>
      </td>
      <td>
        <select name="data[CourseSchedule][schedule_instructor][]" default="" class="form-control select2me instructor_name" id="CourseScheduleScheduleInstructor" style="display: none;">
          <option value="">Select Subject</option>
          <option value="Chintan Mahant" selected="selected">Chintan Mahant</option>
        </select>
      </td>
      <td><input type="button" class="btn btn-primary btn-style" onclick="remove('ScheduleTable1')" name="Delete" value="-"></td>
    </tr>
    <tr id="ScheduleTable0">
      <td><input name="data[CourseSchedule][schedule_from_time][]" class="form-control from_timepicker" readonly="readonly" value="11:54 AM" type="text" id="CourseScheduleScheduleFromTime"></td>
      <td><input name="data[CourseSchedule][schedule_to_time][]" class="form-control to_timepicker" readonly="readonly" value="01:54 AM" type="text" id="CourseScheduleScheduleToTime"></td>
      <td>
        <select name="data[CourseSchedule] 
               [schedule_subject][]" default="" class="form-control select2me 
               course_topic" id="CourseScheduleScheduleSubject">
          <option value="">Select Subject</option>
          <option value="gfgfg" selected="selected">gfgfg</option>
          <option value="Registration">Registration</option>
          <option value="Lunch Break">Lunch Break</option>
          <option value="Tea Break">Tea Break</option>
        </select>
      </td>
      <td>
        <select name="data[CourseSchedule] 
               [schedule_instructor][]" default="" class="form-control select2me 
               instructor_name" id="CourseScheduleScheduleInstructor" style="display: 
               none;">
          <option value="">Select Subject</option>
          <option value="Chintan Mahant" selected="selected">Chintan Mahant
          </option>
        </select>
      </td>
      <td><input type="button" class="btn btn- 
            primary btn-style" id="AddScheduleRow1" name="Add" value="+">
      </td>
    </tr>
  </tbody>
</table>

最佳答案

发生这种情况是因为重复的标识符,id 属性在同一个文档中必须是唯一的。

这将通过用通用类替换重复的类来解决。

那么你的选择器可以很简单:

$(this).closest('tr').find('.instructor_name');

$('body').on('change', '.course_topic', function() {
  var topic_name = $(this).val();
  var names = ['Registration', 'Lunch Break', 'Tea Break'];
  var instructor_name = $(this).closest('tr').find('.instructor_name');

  if ($.inArray(topic_name, names) != -1) {
    instructor_name.hide();
  } else {
    instructor_name.show();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table class="table">
  <tbody>
    <tr>
      <th>From Time</th>
      <th>To Time</th>
      <th>Subject</th>
      <th>Instructor</th>
      <th></th>
    </tr>
  </tbody>
  <tbody id="schedule_table">
    <tr class="ScheduleTable1">
      <td><input name="data[CourseSchedule][schedule_from_time][]" class="form-control from_timepicker" readonly="" value="11:54 AM" type="text"></td>
      <td><input name="data[CourseSchedule][schedule_to_time][]" class="form-control to_timepicker" readonly="readonly" value="01:54 AM" type="text"></td>
      <td>
        <select name="data[CourseSchedule] 
           [schedule_subject][]" default="" class="form-control select2me 
           course_topic">
          <option value="">Select Subject</option>
          <option value="gfgfg" selected="selected">gfgfg</option>
          <option value="Registration">Registration</option>
          <option value="Lunch Break">Lunch Break</option>
          <option value="Tea Break">Tea Break</option>
        </select>
      </td>
      <td>
        <select name="data[CourseSchedule][schedule_instructor][]" default="" class="form-control select2me instructor_name" style="display: none;">
          <option value="">Select Subject</option>
          <option value="Chintan Mahant" selected="selected">Chintan Mahant</option>
        </select>
      </td>
      <td><input type="button" class="btn btn-primary btn-style" onclick="remove('ScheduleTable1')" name="Delete" value="-"></td>
    </tr>
    <tr class="ScheduleTable0">
      <td><input name="data[CourseSchedule][schedule_from_time][]" class="form-control from_timepicker" readonly="readonly" value="11:54 AM" type="text"></td>
      <td><input name="data[CourseSchedule][schedule_to_time][]" class="form-control to_timepicker" readonly="readonly" value="01:54 AM" type="text"></td>
      <td>
        <select name="data[CourseSchedule] 
           [schedule_subject][]" default="" class="form-control select2me 
           course_topic">
          <option value="">Select Subject</option>
          <option value="gfgfg" selected="selected">gfgfg</option>
          <option value="Registration">Registration</option>
          <option value="Lunch Break">Lunch Break</option>
          <option value="Tea Break">Tea Break</option>
        </select>
      </td>
      <td>
        <select name="data[CourseSchedule] 
           [schedule_instructor][]" default="" class="form-control select2me 
           instructor_name" style="display: 
           none;">
          <option value="">Select Subject</option>
          <option value="Chintan Mahant" selected="selected">Chintan Mahant
          </option>
        </select>
      </td>
      <td><input type="button" class="btn btn- 
        primary btn-style" id="AddScheduleRow1" name="Add" value="+">
      </td>
    </tr>
  </tbody>
</table>

关于javascript - 如何在 jquery 中隐藏(或显示)子下拉列表(动态 html)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52533288/

相关文章:

javascript - Html 正文中的样式和脚本

jQuery - 如何在顶部淡出后向上滑动底部?

java - 为什么使用RequestDispatcher时UTF-8在html页面中不起作用?如何修复它?

javascript - OnePage Scroll 自定义导航

javascript - Bing map 上的 HTML 框无法在 IE8 或 Firefox 中正确查看

javascript - 如何将数据从内容脚本传递到 popup.html?

javascript - JSLint 错误 : "Move the invocation into the parens that contain the function"

javascript - 如何将该 anchor 的 Id 作为函数参数发送?

javascript - 数据 URI 在 CSS 中不起作用

javascript - 如何选择第一个和第二个td包含特定值的tr?