根据条件从选择列表中删除值 [英] Remove values from select list based on condition
本文介绍了根据条件从选择列表中删除值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在页面中有以下内容:
I have the following in the page
<select name="val" size="1" >
<option value="A">Apple</option>
<option value="C">Cars</option>
<option value="H">Honda</option>
<option value="F">Fiat</option>
<option value="I">Indigo</option>
</select>
如果某些条件为真,我想从我的选择中删除某些值。
I would like to remove certain values from my select if certain conditions are true.
Eg
E.g
if(frm.product.value=="F"){
// remove Apple and Cars from the select list
}
我该如何使用javascript执行此操作
How can I do this using javascript
推荐答案
为这个select对象赋予一个id:
Give an id for the select object like this:
<select id="mySelect" name="val" size="1" >
<option value="A">Apple</option>
<option value="C">Cars</option>
<option value="H">Honda</option>
<option value="F">Fiat</option>
<option value="I">Indigo</option>
</select>
你可以在纯JavaScript中做到这一点:
You can do it in pure JavaScript:
var selectobject=document.getElementById("mySelect")
for (var i=0; i<selectobject.length; i++){
if (selectobject.options[i].value == 'A' )
selectobject.remove(i);
}
但是 - 正如其他答案所示 - 我强烈建议使用jQuery或其他一些JS库。
But - as the other answers suggest - I'd strongly suggest to use jQuery or some other JS library.
这篇关于根据条件从选择列表中删除值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文