所以我有一个div
,当按下input
按钮时,我可以在其中显示。
<div class="select-list">
<select name="relations" id="selectBox" style="text-transform: none;" class="select" required onchange="otherFunc()">
<option value="">Choose</option>
<option value="Son">Son</option>
<option value="Daughter">Daughter</option>
<option value="Other Relative">Other Relative</option>
<option value="Other">Other</option>
</select>
<span class="highlight"></span>
<span class="bar"></span>
</div>
</div>
<script>
function otherFunc() {
var selectBox = document.getElementById("selectBox");
var selectedValue = selectBox.options[selectBox.selectedIndex].value;
console.log(selectedValue)
if (selectedValue == "Other") {
document.getElementById("other").innerHTML = ' <div class="group"><div class="col-25"><label>Please clarify if "Other"</label></div><div class="col-75"><input type="text" name="otherInput" required></div><span class="highlight"></span><span class="bar"></span></div>'
} else {
}
}
</script>
<div id="other"></div>
然而,现在我想更改它,这样,如果有人点击Other
,但随后将其更改回任何其他选项,它将再次隐藏该div<div id="other"></div>
。我该怎么做?
解决方案#1保留原始代码
function otherFunc() {
var selectBox = document.getElementById("selectBox");
var selectedValue = selectBox.options[selectBox.selectedIndex].value;
console.log(selectedValue)
if (selectedValue == "Other") {
document.getElementById("other").innerHTML = ' <div class="group"><div class="col-25"><label>Please clarify if "Other"</label></div><div class="col-75"><input type="text" name="otherInput" required></div><span class="highlight"></span><span class="bar"></span></div>'
} else {
document.getElementById("other").innerHTML = '';
}
}
<div class="select-list">
<select name="relations" id="selectBox" style="text-transform: none;" class="select" required onchange="otherFunc()">
<option value="">Choose</option>
<option value="Son">Son</option>
<option value="Daughter">Daughter</option>
<option value="Other Relative">Other Relative</option>
<option value="Other">Other</option>
</select>
<span class="highlight"></span>
<span class="bar"></span>
</div>
<div id="other"></div>
解决方案#2你也可以这样做
var other = document.getElementById('other');
other.style.display = 'none';
function otherFunc() {
var selectBox = document.getElementById("selectBox");
var selectedValue = selectBox.options[selectBox.selectedIndex].value;
console.log(selectedValue)
if (selectedValue == "Other") {
other.style.display = 'block';
} else {
other.style.display = 'none';
document.getElementById('otherInput').value = '';
}
}
<div class="select-list">
<select name="relations" id="selectBox" style="text-transform: none;" class="select" required onchange="otherFunc()">
<option value="">Choose</option>
<option value="Son">Son</option>
<option value="Daughter">Daughter</option>
<option value="Other Relative">Other Relative</option>
<option value="Other">Other</option>
</select>
<span class="highlight"></span>
<span class="bar"></span>
</div>
<div id="other">
<div class="group">
<div class="col-25">
<label>Please clarify if "Other"</label>
</div>
<div class="col-75">
<input type="text" name="otherInput" Id="otherInput" required>
</div>
<span class="highlight"></span>
<span class="bar"></span>
</div>
</div>
您只需要在else
中添加document.getElementById("other").innerHTML = '';
即可隐藏div。
<div class="select-list">
<select name="relations" id="selectBox" style="text-transform: none;" class="select" required onchange="otherFunc()">
<option value="">Choose</option>
<option value="Son">Son</option>
<option value="Daughter">Daughter</option>
<option value="Other Relative">Other Relative</option>
<option value="Other">Other</option>
</select>
<span class="highlight"></span>
<span class="bar"></span>
</div>
</div>
<script>
function otherFunc() {
var selectBox = document.getElementById("selectBox");
var selectedValue = selectBox.options[selectBox.selectedIndex].value;
console.log(selectedValue)
if (selectedValue == "Other") {
document.getElementById("other").innerHTML = ' <div class="group"><div class="col-25"><label>Please clarify if "Other"</label></div><div class="col-75"><input type="text" name="otherInput" required></div><span class="highlight"></span><span class="bar"></span></div>'
} else {
document.getElementById("other").innerHTML = '';
}
}
</script>
<div id="other"></div>
好吧,使用一些css类(在元素上设置visibility
、opacity
/其他道具:
<script>
let otherDiv = undefined;
function otherFunc() {
var selectBox = document.getElementById("selectBox");
var selectedValue = selectBox.options[selectBox.selectedIndex].value;
console.log(selectedValue)
if (selectedValue == "Other") {
otherDiv = document.getElementById("other");
otherDiv.style.opacity = 1;
otherDiv.innerHTML = ' <div class="group"><div class="col-25"><label>Please clarify if "Other"</label></div><div class="col-75"><input type="text" name="otherInput" required></div><span class="highlight"></span><span class="bar"></span></div>'
} else {
if(!!otherDiv)
otherDiv.style.opacity = 0;
}
}
</script>
https://plnkr.co/edit/3sT9kwDyp5GBIC0Q?preview