如何使用下面的javascript取出选项值= 0
<select>
<option value=0>0</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
由于
答案 0 :(得分:3)
var select = document.getElementsByTagName("select")[0];
for (var i = 0; i < select.options.length; i++) {
if (select.options[i].value === "0") {
select.remove(i);
}
}
直播example。令人讨厌的是,值是一个字符串,因此您必须===
与字符串进行比较。并且通过tagName获取select,假设它是页面上唯一的选择,很容易失败
使用
<select id="foo"> ... </select>
和
var select = document.getElementById("foo");
会更好。
答案 1 :(得分:1)
如果你可以使用jQuery:
$('select > option[value=0]').remove();
显然,您应该使用select而不是select
的ID,否则您将点击该页面上的所有选择项。