I have a hidden form with several selection fields and some input fields. Click the button to display this form and set some values ββin the fields. The input fields are filled with the given values, but I have a problem with the selection fields.
Using this code:
$("#form select[name=user]").val(value);
with this value gets the attribute "selected" (marked in Firebug), but the option "Select" (initial) is still displayed in the selection field.
I tried to focus and blur after setting the value, but that didn't work either.
Any suggestions?
This is pretty much the standard form:
<form action="#" id="form" class="fix"> <div class="holder"> <label>User:</label> <select name="user" class="styled"> <option value="0" selected>Choose</option> <option value="1">User 1</option> <option value="2">User 2</option> </select> </div> </form>
And calling the jQuery expression:
$("#form select[name=user]").val('2'); $("#form").show();
I get this in Firebug:
<form action="#" id="form" class="fix" style="display:none"> <div class="holder"> <label>User:</label> <select name="user" class="styled"> <option value="0">Choose</option> <option value="1">User 1</option> <option value="2" selected>User 2</option> </select> </div> </form>
but the text selected "Select." If I submit the form, the value will be correctly submitted.
Then, if I reset the form and select an option, the text of the selected option will be displayed correctly. This is strange to me.
jquery select option
Jovica
source share