According to jquery, set the html-select parameter to the element selected by attribute in the text, not the value

I have a select element.

<select class='cSelectType'> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> <option value="4">four</option> <option value="5">five</option> </select> 

I can set the set jquery parameter as follows:

 $('.cSelectType option[value=4]').attr('selected', 'selected'); 

And select the item that displays "Four." To my habit, something like this:

 $('.cSelectType option[xxx=Four]').attr('selected', 'selected'); 

ie The selected attribute is selected in the text set.

some ideas please.

+6
jquery html-select
source share
1 answer

Use the filter :contains() :

Description: Select all items containing the specified text.

 $(".cSelectType option:contains('four')").attr('selected', 'selected'); 
+5
source share

All Articles