开发者

How do you set the selected element of a select html element with jQuery?

开发者 https://www.devze.com 2023-02-28 09:42 出处:网络
I have a <select> html element and I have users create the objects that are then dynamically included in the drop down select list. I want the select menu to be set to newly created object. How

I have a <select> html element and I have users create the objects that are then dynamically included in the drop down select list. I want the select menu to be set to newly created object. How do I set the selected element with开发者_开发知识库 jQuery?


$("#mySelect > #idOfTheOptionIWant").attr("selected", "selected");

The reverse being (to deselect an option):

$("#mySelect > #idOfTheOptionIWant").removeAttr("selected");


Once you have your element selected, simply set the option's 'selected' property to true. Example:

Where your select element has id="foo" and the element in the list you want to be selected is element:

$("#foo").element.selected = true;

That's pretty much it.


Set newOption.selected = true.

0

精彩评论

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