开发者

JQuery manipulation of a SELECT element

开发者 https://www.devze.com 2023-01-25 15:09 出处:网络
I a开发者_运维百科m doing some manipulation of the options contained in a Select element. Is there a way to

I a开发者_运维百科m doing some manipulation of the options contained in a Select element. Is there a way to

  • remove all elements
  • Search an element in the list of already present options
  • update an option attribute (e.g. set as selected)

I am using jQuery 1.4.3


You should look through the API at the html, each, and val methods.

First

$('#mySelect').html('')

Second

$('#mySelect option').each( function() {
     if ($(this).val() == 'valueToChoose') {
        ...
     }
});

Third

$('#mySelect').val('valueToChoose');


Assume that the select statment has id="my_select".

Remove all options:

 $('#my_select').empty()

Find an element:

$('#my_select > option[value=' + value_to_find + '])

Set option as selected (assuming you know the value):

$('#my_select').val(the_value);
0

精彩评论

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