How does jQuery's val(value) set the selected value in a dropdownbox (underwater)
I am working with ASP.NET MVC 3 and jQuery 1.5.
Somewhere in my code I use jQuery to set the selected value of a dropdownbox like so:
$("#SelectedFilterOption").val(filter开发者_开发百科Option)
filterOption
is the value of the option field, not the text. On my screen I see the right value as selected, however, when I look at the html, I do not see any option element with the selected
attribute set. When I post these values to to my ASP.NET MVC 3 app, how do "they" know what value is selected?
Edit:
$("#SelectedFilterOption").val()
does return the right value. Also I noticed that when I set the initial selected value from ASP.NET MVC, that element will get the selected
attribute. The selecte dattribute stays on the field in the html, even when I manually select another option.
I will inspect this with firebug as well (in addition to chrome and IE9 tools).
It finds the index of the first item with that value.
When it has the index, it uses <select>
's .selectedIndex=targetIndexNumber
property via DOM. Editing the html is not required. Some browsers do that themselves, others don't bother.
精彩评论