mirror of
https://github.com/jquery/jquery.git
synced 2026-02-03 01:15:30 -05: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:
committed by
Richard Gibson
parent
8f4572406c
commit
c9ca9bf509
@@ -272,13 +272,20 @@ jQuery.extend({
|
||||
},
|
||||
|
||||
set: function( elem, value ) {
|
||||
var values = jQuery.makeArray( value );
|
||||
|
||||
jQuery(elem).find("option").each(function() {
|
||||
this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
|
||||
});
|
||||
|
||||
if ( !values.length ) {
|
||||
var optionSet, option,
|
||||
options = elem.options,
|
||||
values = jQuery.makeArray( value ),
|
||||
i = options.length;
|
||||
|
||||
while ( i-- ) {
|
||||
option = options[ i ];
|
||||
if ( (option.selected = jQuery.inArray( jQuery(option).val(), values ) >= 0) ) {
|
||||
optionSet = true;
|
||||
}
|
||||
}
|
||||
|
||||
// force browsers to behave consistently when non-matching value is set
|
||||
if ( !optionSet ) {
|
||||
elem.selectedIndex = -1;
|
||||
}
|
||||
return values;
|
||||
|
||||
Reference in New Issue
Block a user