Opened 15 years ago
Closed 12 years ago
#3104 closed bug (worksforme)
setting a value on a select element ambiguous
Reported by: | Borre | Owned by: | |
---|---|---|---|
Priority: | major | Milestone: | 1.4.3 |
Component: | attributes | Version: | 1.4.2 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
given a select box:
<select id="test_dropdown"> <option value="1">somevalue</option> <option value="2">1</option> </select>
and given the statement
$('#test_dropdown').val('1');
the second option will be selected. This seems to be unwanted behaviour (in my opinion, an option with a value attribute should have priority). The erroneous code seems to be located in jquery-1.2.6.js on line 419:
jQuery( "option", this ).each(function(){ this.selected = (jQuery.inArray( this.value, values ) >= 0 || jQuery.inArray( this.text, values ) >= 0); });
Change History (5)
comment:1 Changed 14 years ago by
comment:2 Changed 14 years ago by
Resolution: | → fixed |
---|---|
Status: | new → closed |
It sounds like this was fixed in jQuery 1.3.
comment:3 Changed 14 years ago by
Resolution: | fixed |
---|---|
Status: | closed → reopened |
I'm sorry to say it was not. The problem still exists.
The solution that works for me is replacing jquery-1.3.1.js, line 462-465:
jQuery( "option", this ).each(function(){ this.selected = (jQuery.inArray( this.value, values ) >= 0 || jQuery.inArray( this.text, values ) >= 0); });
with:
var selectedFound = false; jQuery( "option", this ).each(function(){ selectedFound = selectedFound || (this.selected = (jQuery.inArray( this.value, values ) >= 0)); }); if(!selectedFound){ jQuery( "option", this ).each(function(){ this.selected = (jQuery.inArray( this.text, values ) >= 0); }); }
comment:4 Changed 13 years ago by
Component: | core → attributes |
---|
comment:5 Changed 12 years ago by
Milestone: | 1.3 → 1.4.3 |
---|---|
Resolution: | → worksforme |
Status: | reopened → closed |
Version: | 1.2.6 → 1.4.2 |
This was resolved in 1.4. More tests here to confirm: http://github.com/jquery/jquery/commit/879799fe955f01b85b59fd8a0096d415fc48df03
Note: See
TracTickets for help on using
tickets.
I confirm this unwanted behavior. As per W3 standard regarding the 'value' attribute of OPTION:
"This attribute specifies the initial value of the control. If this attribute is not set, the initial value is set to the contents of the OPTION element."