2010-11-30 112 views
4

如何使用jquery删除opt1行?如何更改opt2成为被选中的? 请注意,这些值是随机数字。如何使用jquery从选择框中删除选项

 <select name="ShippingMethod" > 
     <option value="8247(random)">Opt2</option> 
     <option value="1939(random)" selected="selected">Opt1</option> 
     </select> 

回答

12

这取决于你想怎么选择它,to remove by text

$("select[name=ShippingMethod] option").filter(function() { 
    return this.text == "Opt1"; 
}).remove(); 

Or the selected one

$("select[name=ShippingMethod] option:selected").remove(); 

Or the second one

$("select[name=ShippingMethod] option:eq(1)").remove(); 

Or the last one

$("select[name=ShippingMethod] option:last").remove(); 

只选择选项2的文字,你可以用同样的方法.filter()上面:

$("select[name=ShippingMethod] option").filter(function() { 
    return this.text == "Opt2"; 
}).attr("selected", true); 

You can test it here

0

快速猜测,问题2

$('select[name=ShippingMethod]').val('19395ss'); 

尼克·卡弗似乎已经回答了第一个问题。

相关问题