开发者

How to set drop down list option up to specific value

开发者 https://www.devze.com 2022-12-20 09:21 出处:网络
If I have valvo that obtain from database, How can I set volvo option status into act开发者_开发问答ive(checked) by using JQuery?

If I have valvo that obtain from database, How can I set volvo option status into act开发者_开发问答ive(checked) by using JQuery?

<select name="car">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="mercedes">Mercedes</option>
      <option value="audi">Audi</option>
</select>

I don't know how to manager control that have same identity.

??? $('input[name="car"]').attr('checked', true);


This also works:

$("select[name=car]").val('volvo');


Since your example is a drop down list you have to use val()

$("select[name='car']").val('volvo');

If you want to use radio button then you can use

$(":radio[value='volvo']").attr ( "checked" , true );

for the following HTML

<input type="radio" value="volvo" />
<input type="radio" value="saab" />
<input type="radio" value="mercedes" />
<input type="radio" value="audi" />


$('select[name="car"]').val("volvo");

A select box is different to a radio button, it doesn't have a 'checked' attribute. The above works, although the specific attribute you want to set on the option element is selected:

$('select[name="car"]').attr("selected", "selected");


Does JQuery support multiple CSS attribute selectors? If so, this should work:

$('input[name=car][value=volvo]').attr('checked', true);

Otherwise, you can always loop through all available radio buttons, check their value, and check them when the value matches.


This should work for radio buttons as stated on your original question.

$('input[name=car]:radio').attr('checked','checked');

This should work for select

$('select[name=car]').val('volvo');
0

精彩评论

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

关注公众号