如何隐藏基于复选框的值,选中时需要包含在内(包括下拉框版本)

Lim*_*Lim 1 html javascript checkbox jquery dropdownbox

我希望在选中复选框时显示文本区域,并在未选中时隐藏它们。界面可以运行,但复选框不可点击。

$(document).ready(function() {
  $('#ifbroken').change(function() {
    if (this.checked)
      $('#dvchk').fadeIn('slow');
    else
      $('#dvchk').fadeOut('slow');
  })
});
Run Code Online (Sandbox Code Playgroud)
#dvchk {
  display: none
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous">
</script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<div class="input-field col s12">
  <input type="checkbox" id="ifbroken">
  <label for="ifborken">If Broken</label>
</div>

<div class="input-field col s12" id="dvchk">
  <label for="Problem">Problem</label></br>
  </br>
  <textarea name="Problem" style="width:600px; height:200px;"></textarea>
</div>

<div class="input-field col s12" id="dvchk">
  <label for="ActionTaken">Action Taken</label></br>
  </br>
  <textarea name="ActionTaken" style="width:600px; height:200px;"></textarea>
</div>

<div class="input-field col s12" id="dvchk">
  <label for="BuyOff">Buy Off</label></br>
  </br>
  <textarea name="BuyOff" style="width:600px; height:200px;"></textarea>
</div>
Run Code Online (Sandbox Code Playgroud)

mpl*_*jan 5

您有多个相同的 ID 和无效的 HTML 以及加载了过多的 jQuery 文件

这有效

我将 ID 更改为类,修复了标签中的错字和无效的 </br>

我还将内联样式移动到样式表中

function toggleField() {
  $fld = $(".dvchk").find(":input").prop("required", this.checked);

  if (this.checked) $('.dvchk').fadeIn('slow'); // there is alas no fadeToggle(boolean)
  else $('.dvchk').fadeOut('slow');
}
$(function() {
  $('#ifbroken').on("click", toggleField)
});
Run Code Online (Sandbox Code Playgroud)
.dvchk {
  display: none
}

textarea {
  width: 600px;
  height: 200px;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<form>
  <div class="input-field col s12">
    <input type="checkbox" id="ifbroken">
    <label for="ifbroken">If Broken</label>
  </div>

  <div class="input-field col s12 dvchk">
    <label for="Problem">Problem</label><br /><br />
    <textarea name="Problem"></textarea>
  </div>

  <div class="input-field col s12 dvchk">
    <label for="ActionTaken">Action Taken</label><br /><br />
    <textarea name="ActionTaken"></textarea>
  </div>

  <div class="input-field col s12 dvchk">
    <label for="BuyOff">Buy Off</label><br /><br />
    <textarea name="BuyOff"></textarea>
  </div>
  <input class="dvchk" type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)