从页面加载下拉列表中删除项目(无jquery)
问题描述:
我有以下的下拉式清单:
I have the following dropdown list:
<select name="DD1" id="DD1">
<option value="B">B</option>
<option value="D">D</option>
<option value="E">E</option>
<option value="F">F</option>
<option value="R">R</option>
</select>
在页面加载时,我需要隐藏/删除选项D.我不能按索引去该列表是动态的,所以我必须使用value参数或渲染的实际文本。
On page load I need to hide/delete option D. I can't go by the index because that list is dynamic so I have to use the value parameter or the actual text that gets rendered.
我试过找到答案,但是我遇到的所有解决方案都是使用JQuery来完成的,我没有这个选项。
I've tried finding the answer but all the solutions I came across use JQuery for this and I don't have the option to do this.
任何人都有办法在页面加载时使用Javascipt隐藏选项D,所以用户从不会看到该选项?
Anyone have a way to hide option D just using Javascipt on page load so the user never sees that option?
p>
答
var select=document.getElementById('DD1');
for (i=0;i<select.length; i++) {
if (select.options[i].value=='D') {
select.remove(i);
}
}