如果我在輸入框中輸入一個值並單擊按鈕。我想檢查下拉列表中的輸入值,如果這些值存在於下拉列表中,我想刪除該選項,否則我想在選項中添加輸入值。根據輸入值添加和刪除下拉列表中的選項
HTML:
<input type="text" id="inputValue" />
<button id="myBtn" onclick="validate()">Click</button>
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option>Mango</option>
<option>Lemon</option>
</select>
JS:
function validate() {
var ipValue = document.getElementById('inputValue').value;
var select = document.getElementById('mySelect');
for(i=0;i<select.length;i++) {
if(select[i] == ipValue) {
ipValue.removeChild(select[i]);
}
else {
var option = document.createElement('option');
select.appendChild(option);
option.innerHTML = ipValue;
}
}
}
問題是什麼,什麼是您會收到錯誤? –
@pascal betz我無法使用下拉菜單檢查輸入值。我想檢查輸入的值在下拉列表中..如果值exixts刪除選項,否則要添加元素。 –