jQuery: Setting select list 'selected' based on text, failing strangely

前端 未结 11 1470
野的像风
野的像风 2020-12-04 17:31

I have been using the following code (with jQuery v1.4.2) to set the \'selected\' attribute of a select list based on its \'text\' description rather than its \'value\':

11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 18:14

    Using the filter() function seems to work in your test cases (tested in Firefox). The selector would look like this:

    $('#mySelect1 option').filter(function () {
        return $(this).text() === 'Banana';
    });
    

提交回复
热议问题