mirror of
https://github.com/jquery/jquery.git
synced 2025-01-10 18:24:24 +00:00
Fix #13514: Set selectedIndex to -1 when non-matching value is set on a select. Close gh-1191.
(cherry picked from commit 48d71d0c3e
)
This commit is contained in:
parent
8f4572406c
commit
c9ca9bf509
@ -272,13 +272,20 @@ jQuery.extend({
|
|||||||
},
|
},
|
||||||
|
|
||||||
set: function( elem, value ) {
|
set: function( elem, value ) {
|
||||||
var values = jQuery.makeArray( value );
|
var optionSet, option,
|
||||||
|
options = elem.options,
|
||||||
|
values = jQuery.makeArray( value ),
|
||||||
|
i = options.length;
|
||||||
|
|
||||||
jQuery(elem).find("option").each(function() {
|
while ( i-- ) {
|
||||||
this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
|
option = options[ i ];
|
||||||
});
|
if ( (option.selected = jQuery.inArray( jQuery(option).val(), values ) >= 0) ) {
|
||||||
|
optionSet = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( !values.length ) {
|
// force browsers to behave consistently when non-matching value is set
|
||||||
|
if ( !optionSet ) {
|
||||||
elem.selectedIndex = -1;
|
elem.selectedIndex = -1;
|
||||||
}
|
}
|
||||||
return values;
|
return values;
|
||||||
|
@ -846,6 +846,22 @@ test( "val()", function() {
|
|||||||
equal( jQuery("<option/>").val("test").attr("value"), "test", "Setting value sets the value attribute" );
|
equal( jQuery("<option/>").val("test").attr("value"), "test", "Setting value sets the value attribute" );
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("val() with non-matching values on dropdown list", function() {
|
||||||
|
expect( 3 );
|
||||||
|
|
||||||
|
jQuery("#select5").val( "" );
|
||||||
|
equal( jQuery("#select5").val(), null, "Non-matching set on select-one" );
|
||||||
|
|
||||||
|
var select6 = jQuery("<select multiple id=\"select6\"><option value=\"1\">A</option><option value=\"2\">B</option></select>").appendTo("#form");
|
||||||
|
jQuery(select6).val( "nothing" );
|
||||||
|
equal( jQuery(select6).val(), null, "Non-matching set (single value) on select-multiple" );
|
||||||
|
|
||||||
|
jQuery(select6).val( ["nothing1", "nothing2"] );
|
||||||
|
equal( jQuery(select6).val(), null, "Non-matching set (array of values) on select-multiple" );
|
||||||
|
|
||||||
|
select6.remove();
|
||||||
|
});
|
||||||
|
|
||||||
if ( "value" in document.createElement("meter") &&
|
if ( "value" in document.createElement("meter") &&
|
||||||
"value" in document.createElement("progress") ) {
|
"value" in document.createElement("progress") ) {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user