如何使用jquery删除opt1行?更改 opt2 以使其被选中怎么样? 请注意,这些值是随机数。
<select name="ShippingMethod" >
<option value="8247(random)">Opt2</option>
<option value="1939(random)" selected="selected">Opt1</option>
</select>
最佳答案
这取决于你想如何选择它,to remove by text :
$("select[name=ShippingMethod] option").filter(function() {
return this.text == "Opt1";
}).remove();
$("select[name=ShippingMethod] option:selected").remove();
$("select[name=ShippingMethod] option:eq(1)").remove();
$("select[name=ShippingMethod] option:last").remove();
<小时/>
要通过文本选择选项 2,您可以使用相同的 .filter()
上面的方法:
$("select[name=ShippingMethod] option").filter(function() {
return this.text == "Opt2";
}).attr("selected", true);
关于jquery - 如何使用jquery从选择框中删除选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4313639/