試圖在下拉選單中創建一個“其他”選項,這樣做會激活一個文本框以輸入。但是,我當前的這樣做方法會導致其余選項在單擊時不可見,并且文本框不會出現在單擊其他選項。
function disappear(val) {
var textbox = document.getElementById('issue');
if (val == 'other') {
textbox.style.display = 'block';
} else {
textbox.style.display = 'none';
}
}
<select name="issue" id="issue" onchange='disappear(this.value)'>
<option disabled selected></option>
<option value="bug">Bug</option>
<option value="lacking">Lack of Use</option>
<option value="other">Other</option>
</select>
<div id="textbox"></div>
uj5u.com熱心網友回復:
您選擇了錯誤的元素
var textbox=document.getElementById('textbox');
function disappear(val){
var textbox=document.getElementById('textbox');
if(val=='other'){
textbox.style.display='block';
}
else{
textbox.style.display='none';
}
}
#textbox{
width:90px;
height:60px;
background-color:red;
display:none;
}
<select name="issue" id="issue" onchange='disappear(this.value)'>
<option disabled selected></option>
<option value="bug">Bug</option>
<option value="lacking">Lack of Use</option>
<option value="other">Other</option>
</select>
<div id="textbox"></div>
uj5u.com熱心網友回復:
這就是你必須這樣做的方式:
var textbox=document.getElementById('textbox');
uj5u.com熱心網友回復:
你真的很親近!唯一剩下的就是將 id 中的 id 更改為document.getElementById正確的元素并添加帶有樣式的輸入欄位。
function disappear(val) {
var textbox = document.getElementById('textbox');
if (val == 'other') {
textbox.style.display = 'block';
} else {
textbox.style.display = 'none';
}
}
<select name="issue" id="issue" onchange='disappear(this.value)'>
<option disabled selected></option>
<option value="bug">Bug</option>
<option value="lacking">Lack of Use</option>
<option value="other">Other</option>
</select>
<input id="textbox" type="text" style="display: none;">
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/507564.html
標籤:javascript html cs50
