如何使用jquery从选择框中删除选项

时间:2010-11-30 12:17:37

标签: jquery

如何使用jquery删除opt1行?如何将opt2更改为选中状态? 请注意,这些值是随机数。

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

2 个答案:

答案 0 :(得分:14)

这取决于您要如何选择它,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

答案 1 :(得分:0)

快速猜测问题二

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

Nick Carver似乎回答了第一个问题。

相关问题