javascript - 如何在单击复选框时创建下拉菜单?

标签 javascript

这是我的复选框和下拉菜单:

<label>
  <input type="checkbox" name="check_list[]" Value="Castle Connolly Regional Top Doctor" />Castle Connolly Regional Top Doctor</label>
<select name="check_year[]"></select>
<label>
  <input type="checkbox" name="check_list[]" Value="Castle Connolly Top U.S. Doctor" id="ccn" />Castle Connolly Top U.S. Doctor</label>
<select name="check_year[]"></select>
<label>
  <input type="checkbox" name="check_list[]" Value="Vitals Compassionate Doctor" />Vital's Compassionate Doctor Recognition</label>
<select name="check_year[]"></select>
<label>
  <input type="checkbox" name="check_list[]" Value="Super Doctor" />Super Doctor</label>
<select name="check_year[]"></select>

我最初尝试将它们捕获在数组中,然后将它们连接到一个变量中,然后插入到数据库中,但是如果数组的长度不同,那就很困难。我认为最好只在选择其中一个框时才显示下拉菜单,那么我如何在 JavaScript 中做到这一点呢?

最佳答案

我认为使用 jQuery 实现这一目标的最简单方法是:

// select the <input> elements whose 'type' attribute is equal
// to 'checkbox', and bind the anonymous function of the on()
// method as the event handler for the named ('change') event(s):
$('input[type=checkbox]').on('change', function(){

  // find the first ancestor <label> element of the current
  // <input>, find the next sibling that matches the selector
  // (to ensure that we're working only with <select> elements,
  // and set its 'disabled' property to false when the the
  // checkbox is checked (so !true) and to true when the checkbox
  // is not checked:
  $(this).closest('label').next('select').prop('disabled', !this.checked);

// fire the change event on each of the checkboxes on page-load,
// to appropriately disable/enable the <select> elements:
}).change();

$('input[type=checkbox]').on('change', function() {
  $(this).closest('label').next('select').prop('disabled', !this.checked);
}).change();
label {
  margin: 0.5em 1em 0 0;
}
label::before {
  content: '';
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
  <input type="checkbox" name="check_list[]" Value="Castle Connolly Regional Top Doctor" />Castle Connolly Regional Top Doctor</label>
<select name="check_year[]"></select>
<label>
  <input type="checkbox" name="check_list[]" Value="Castle Connolly Top U.S. Doctor" id="ccn" />Castle Connolly Top U.S. Doctor</label>
<select name="check_year[]"></select>
<label>
  <input type="checkbox" name="check_list[]" Value="Vitals Compassionate Doctor" />Vital's Compassionate Doctor Recognition</label>
<select name="check_year[]"></select>
<label>
  <input type="checkbox" name="check_list[]" Value="Super Doctor" />Super Doctor</label>
<select name="check_year[]"></select>

JS Fiddle demo .

引用文献:

关于javascript - 如何在单击复选框时创建下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37400668/

相关文章:

javascript - var foo = foo ||警报(富);

php - 将 href (html) 标签与 PHP 一起使用

java - 我怎样才能从它的大小找到大致的歌曲时间

javascript - 在 Jasmine 中每次测试后如何自动恢复所有 sinon.js spy ?

javascript - 我们需要使用 javascript 来创建响应式布局吗?

javascript - 异步返回值

javascript - 尽管有 takeUntil,定时器的 Observable 永远不会完成

javascript - 从网页 Java 中删除部分网站

javascript - 类型错误 : Cannot read property 'nombre' of undefined

javascript - 动态更新 Float Left Div 的宽度