仅在选择特定选项时显示输入字段
Show input field only if a specific option is selected
我正在尝试创建一个表单来检查是否从 "select" 标签中选择了某个选项。这是我目前的 HTML:
<select onchange="yesnoCheck()">
<option id="noCheck" value="">Valitse automerkkisi</option>
<option id="noCheck" value="lada">Lada</option>
<option id="noCheck" value="mosse">Mosse</option>
<option id="noCheck" value="volga">Volga</option>
<option id="noCheck" value="vartburg">Vartburg</option>
<option id="yesCheck" value="other">Muu</option>
</select>
这是 div 元素,在选择 "Muu" 后应该可见:
<div id="ifYes" style="display: none;">
<label for="car">Muu, mikä?</label> <input type="text" id="car" name="car" /><br />
</div>
这是我正在尝试使用的 JavaScript:
<script type="text/javascript">
function yesnoCheck() {
if (document.getElementById("yesCheck").checked) {
document.getElementById("ifYes").style.display = "block";
} else {
document.getElementById("ifYes").style.display = "none";
}
}
</script>
但是没用...
查看:Display div if a specific select option value is selected
也不要使用 ID 使用数据属性或值来检查比较。
给你:
function yesnoCheck(that) {
if (that.value == "other") {
alert("check");
document.getElementById("ifYes").style.display = "block";
} else {
document.getElementById("ifYes").style.display = "none";
}
}
<select onchange="yesnoCheck(this);">
<option value="">Valitse automerkkisi</option>
<option value="lada">Lada</option>
<option value="mosse">Mosse</option>
<option value="volga">Volga</option>
<option value="vartburg">Vartburg</option>
<option value="other">Muu</option>
</select>
<div id="ifYes" style="display: none;">
<label for="car">Muu, mikä?</label> <input type="text" id="car" name="car" /><br />
</div>
<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code> function yesnoCheck(that) {
if (that.value == "other") {
document.getElementById("ifYes").style.display = "block";
}elseif (that.value == "lada") {
} else {
document.getElementById("ifYes").style.display = "none";
}
}
<select onchange="yesnoCheck(this);">
<option value="">Valitse automerkkisi</option>
<option value="lada">Lada</option>
<option value="mosse">Mosse</option>
<option value="volga">Volga</option>
<option value="vartburg">Vartburg</option>
<option value="other">Muu</option>
</select>
<div id="ifYes" style="display: none;">
<label for="car">Muu, mikä?</label> <input type="text" id="car" name="car" /><br />
</div>
我正在尝试创建一个表单来检查是否从 "select" 标签中选择了某个选项。这是我目前的 HTML:
<select onchange="yesnoCheck()">
<option id="noCheck" value="">Valitse automerkkisi</option>
<option id="noCheck" value="lada">Lada</option>
<option id="noCheck" value="mosse">Mosse</option>
<option id="noCheck" value="volga">Volga</option>
<option id="noCheck" value="vartburg">Vartburg</option>
<option id="yesCheck" value="other">Muu</option>
</select>
这是 div 元素,在选择 "Muu" 后应该可见:
<div id="ifYes" style="display: none;">
<label for="car">Muu, mikä?</label> <input type="text" id="car" name="car" /><br />
</div>
这是我正在尝试使用的 JavaScript:
<script type="text/javascript">
function yesnoCheck() {
if (document.getElementById("yesCheck").checked) {
document.getElementById("ifYes").style.display = "block";
} else {
document.getElementById("ifYes").style.display = "none";
}
}
</script>
但是没用...
查看:Display div if a specific select option value is selected
也不要使用 ID 使用数据属性或值来检查比较。
给你:
function yesnoCheck(that) {
if (that.value == "other") {
alert("check");
document.getElementById("ifYes").style.display = "block";
} else {
document.getElementById("ifYes").style.display = "none";
}
}
<select onchange="yesnoCheck(this);">
<option value="">Valitse automerkkisi</option>
<option value="lada">Lada</option>
<option value="mosse">Mosse</option>
<option value="volga">Volga</option>
<option value="vartburg">Vartburg</option>
<option value="other">Muu</option>
</select>
<div id="ifYes" style="display: none;">
<label for="car">Muu, mikä?</label> <input type="text" id="car" name="car" /><br />
</div>
<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code> function yesnoCheck(that) {
if (that.value == "other") {
document.getElementById("ifYes").style.display = "block";
}elseif (that.value == "lada") {
} else {
document.getElementById("ifYes").style.display = "none";
}
}
<select onchange="yesnoCheck(this);">
<option value="">Valitse automerkkisi</option>
<option value="lada">Lada</option>
<option value="mosse">Mosse</option>
<option value="volga">Volga</option>
<option value="vartburg">Vartburg</option>
<option value="other">Muu</option>
</select>
<div id="ifYes" style="display: none;">
<label for="car">Muu, mikä?</label> <input type="text" id="car" name="car" /><br />
</div>