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);
精彩评论