开发者

Is it possible to change the selected based on a string of the text?

开发者 https://www.devze.com 2023-01-19 03:10 出处:网络
I am trying to set the select option to selected based on its text $(\"#request_showdate_2i\").val(\"November\");

I am trying to set the select option to selected based on its text

$("#request_showdate_2i").val("November");


<select name="request[showdate(2i)]" id="request_showdate_2i">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<op开发者_如何转开发tion value="10" selected="selected">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>

nothing changes...I dont have the value portion of the month available to me, just the string...any suggestions


Well, using the :contains() selector works here:

var month = "December";
$("#request_showdate_2i > option:contains("+ month +")").attr('selected', true);

See: http://www.jsfiddle.net/yijiang/YEXZt/


$("#request_showdate_2i").val(
    $("#request_showdate_2i option:contains('October')").attr('value')    
);


Why do you need to do this? Where are you getting "November" from? If you are typing it in yourself, then you can just use the numbers instead for val.

Alternatively, an option will fall back on its inner text as its val if no value attribute is defined. That is to say your jQuery above will work fine if you drop the value attributes. Any reason you would still need the numbers?

0

精彩评论

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