开发者

selecting option in a <select> using JQuery

开发者 https://www.devze.com 2023-02-11 03:23 出处:网络
How do you use JQuery to make an option in aobject selected?Here is my: <select id=\'ddlMeter\'>

How do you use JQuery to make an option in a object selected? Here is my :

    <select id='ddlMeter'>
<option value='#ff3333' style='background-color:#ff3333; color:#ff3333'>red</option>
<option value='#ffee44' style='background-color:#ffee44开发者_运维问答; color:#ffee44'>amber</option>
<option value='#33ee66' style='background-color:#33ee66; color:#33ee66'>green</option>
<option value='#333333' style='background-color:#333333; color:#333333'>black</option>
</select>


You need to set the Attribute selected. You can do this by invoking .attr()help.

Example:

$('select option:eq(1)').attr('selected', true);

will select the second option. As an alternative, you may set the .val()help on the parent <option> element.

$('select').val('#ffee44');


$("#ddlMeter").val("#33ee66");


To mark an option selected, you can just set the val() of the select item.

$('#ddlMeter').val('#333333');  //black is selected
0

精彩评论

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