2010-11-30 118 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'); 

尼克·卡弗似乎已經回答了第一個問題。

相關問題