javascript - 在特定值时下拉不显示内容

标签 javascript html css

我正在尝试在值为 2 时制作一个下拉菜单显示我的其他 div。现在我的下拉菜单是“事件有证人吗?”然后有两个选项“否”为值 1,"is"为值 2。因此,如果选择值 2"is",则会显示另外两个问题“证人姓名”和“证人联系电话”。但我似乎无法弄清楚为什么它不起作用,任何人都可以弄清楚为什么会这样吗?

再次感谢,

马特。

$(window).on("load", function() {
  $('p select[name=dd_1]').change(function(e) {
    if ($('p select[name=dd_1]').val() == '2') {
      $('#irMainWitness').show();
      $('#irQMainWitness').show();
      $('#irBottomWitness').show();
    } else {
      $('#irMainWitness').hide();
      $('#irQMainWitness').hide();
      $('#irBottomWitness').hide();
    }
  });
});
p {
  margin: 0;
}
textarea {
  overflow: hidden;
  resize: none;
  border: none;
  overflow: auto;
  outline: none;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
  height: 40px;
  width: 98%;
  margin: 0;
  background: #fff;
}
.dropDown {
  width: 99.5%;
  height: 46px;
  margin: 0;
  border: none;
  background: none;
}
#irMain {
  background-color: #ddebf7;
  border-top: 2px solid #000;
  border-left: 2px solid #000;
  border-right: 2px solid #000;
  text-align: center;
  font-weight: bold;
  margin: 0;
  height: 30px;
}
#irQMain {
  background-color: #FFF;
  border: 2px solid #000;
  text-align: center;
  font-weight: bold;
  margin: 0;
  height: 50px;
}
#irMainWitness {
  background-color: #ddebf7;
  border-left: 2px solid #000;
  border-right: 2px solid #000;
  text-align: center;
  font-weight: bold;
  margin: 0;
  height: 30px;
  display: none;
}
#irQMainWitness {
  background-color: #FFF;
  border-top: 2px solid #000;
  border-bottom: 2px solid #000;
  border-left: 2px solid #000;
  border-right: 2px solid #000;
  text-align: center;
  font-weight: bold;
  margin: 0;
  height: 50px;
  display: none;
}
#irBottomWitness {
  background-color: #FFF;
  border: 2px solid #000;
  height: 50px;
  margin: 0;
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="irMain">
  <p>Was there a Witness to the Incident?:</p>
</div>
<div id="irQMain">
  <select name="dd_1" class="dropDown">
    <option value="1">No</option>
    <option value="2">Yes</option>
  </select>
</div>

<div id="irMainWitness">
  <p>Name of Witness:</p>
</div>

<div id="irQMainWitness">
  <textarea name="tb_16" cols="1" rows="2"></textarea>
</div>

<div id="irMainWitness">
  <p>Contact Number of Witness:</p>
</div>

<div id="irBottomWitness">
  <textarea name="tb_17" cols="1" rows="2"></textarea>
</div>

最佳答案

您只需要确保您的选择器是正确的,您可以通过将您的名称值明确地用引号括在您的选择器中而不是将其范围限定在 <p> 内来实现。标记(因为您的标记中目前没有):

$('select[name="dd_1"]').change(function(e) { ... });

此外,您应该能够通过使用 toggle() 显着简化现有代码。功能:

$(function() {
  $('select[name="dd_1"]').change(function(e) {
    $("#irMainWitness,#irQMainWitness,#irBottomWitness").toggle($(this).val() == '2');
  });
});

示例

enter image description here

$(function() {
  $('select[name="dd_1"]').change(function(e) {
    $("#irMainWitness,#irQMainWitness,#irBottomWitness").toggle($(this).val() == '2');
  });
});
p {
  margin: 0;
}
textarea {
  overflow: hidden;
  resize: none;
  border: none;
  overflow: auto;
  outline: none;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
  height: 40px;
  width: 98%;
  margin: 0;
  background: #fff;
}
.dropDown {
  width: 99.5%;
  height: 46px;
  margin: 0;
  border: none;
  background: none;
}
#irMain {
  background-color: #ddebf7;
  border-top: 2px solid #000;
  border-left: 2px solid #000;
  border-right: 2px solid #000;
  text-align: center;
  font-weight: bold;
  margin: 0;
  height: 30px;
}
#irQMain {
  background-color: #FFF;
  border: 2px solid #000;
  text-align: center;
  font-weight: bold;
  margin: 0;
  height: 50px;
}
#irMainWitness {
  background-color: #ddebf7;
  border-left: 2px solid #000;
  border-right: 2px solid #000;
  text-align: center;
  font-weight: bold;
  margin: 0;
  height: 30px;
  display: none;
}
#irQMainWitness {
  background-color: #FFF;
  border-top: 2px solid #000;
  border-bottom: 2px solid #000;
  border-left: 2px solid #000;
  border-right: 2px solid #000;
  text-align: center;
  font-weight: bold;
  margin: 0;
  height: 50px;
  display: none;
}
#irBottomWitness {
  background-color: #FFF;
  border: 2px solid #000;
  height: 50px;
  margin: 0;
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="irMain">
  <p>Was there a Witness to the Incident?:</p>
</div>
<div id="irQMain">
  <select name="dd_1" class="dropDown">
    <option value="1">No</option>
    <option value="2">Yes</option>
  </select>
</div>

<div id="irMainWitness">
  <p>Name of Witness:</p>
</div>

<div id="irQMainWitness">
  <textarea name="tb_16" cols="1" rows="2"></textarea>
</div>

<div id="irMainWitness">
  <p>Contact Number of Witness:</p>
</div>

<div id="irBottomWitness">
  <textarea name="tb_17" cols="1" rows="2"></textarea>
</div>

关于javascript - 在特定值时下拉不显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41701857/

相关文章:

html - 使用绝对和顶部将页脚定位在页面底部

javascript - 单击时更改按钮文本(更多/更少)

javascript - 回调函数不等待 jQuery fadeOut() 完成?

html - 使用 Spotify webAPI 播放艺术家 (ID) 的随机专辑

javascript - 作用域未在处于不同状态的函数内更新

javascript - 单击停止视频幻灯片

jquery - 将内联元素扩展到其自动宽度

javascript - 悬停状态为文本时预加载图像的最佳方式?

javascript - ReactJS 中的 Bootstrap 行

float 值的 Javascript 验证