简体   繁体   English

从javascript中取出选项

[英]take out option from javascript

how to take out option value = 0 using javascript from below 如何从下面使用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>

Thanks 谢谢

var select = document.getElementsByTagName("select")[0];
for (var i  = 0; i < select.options.length; i++) {
    if (select.options[i].value === "0") {
        select.remove(i);
    }
}

See a live example . 看到一个真实的例子 Annoyingly the value is a string so you have to === compare to the string. 令人讨厌的是,该值是一个字符串,因此您必须===与该字符串进行比较。 And getting the select by tagName assuming it's the only select on the page is prone to failure 假设它是页面上的唯一选择,则通过tagName获取选择很容易失败

using 运用

<select id="foo"> ... </select>

and

var select = document.getElementById("foo");

Would be better. 会更好。

If you can use jQuery: 如果可以使用jQuery:

$('select > option[value=0]').remove();

Obviously you should use the id of the select instead of select or you will hit all select items on that page. 显然,您应该使用select的ID而不是select否则您将点击该页面上的所有select项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM