Selecting an option in a select control by its value is a fairly straightforward task in jQuery. However, if you need to set the selected element based on its text description, the process can be slightly more complex.
Using jQuery v1.6
For jQuery versions 1.6 and above, you can use the following code to set the selected option by its text:
<code class="js">var text1 = 'Two'; $("select option").filter(function() { return $(this).text() == text1; }).prop('selected', true);</code>
This code snippet first uses the filter() method to select the option element with the matching text description (in this case, "Two"). It then sets the selected property of the selected option to true, effectively marking it as the selected option.
The above is the detailed content of How to Select an Option in a Select Control by Text Description Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!