开发者

how to use jquery to remove option from select box

开发者 https://www.devze.com 2023-01-28 16:40 出处:网络
How to use jquery to delete opt1 row?How about changing opt2 to become selected? Note that the values are random numbers.

How to use jquery to delete opt1 row ? How about changing opt2 to become selected? Note that the values are random numbers.

      <select name="ShippingMethod" >
        <option value="8247(r开发者_StackOverflowandom)">Opt2</option>
        <option value="1939(random)" selected="selected">Opt1</option>
      </select>


It depends how you want to select it, 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();

To just select option 2 by text, you can use the same .filter() approach above:

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

You can test it here.


Quick guess as to question two

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

Nick Carver seems to have answered the first question.

0

精彩评论

暂无评论...
验证码 换一张
取 消