diff options
author | ruado1987 <ruado1987@gmail.com> | 2013-03-03 23:51:04 +0800 |
---|---|---|
committer | Richard Gibson <richard.gibson@gmail.com> | 2013-03-22 12:46:13 -0400 |
commit | 48d71d0c3e53d1bb1688fc6c0593b008ced3403b (patch) | |
tree | ce43c6e0dfae7b2375a552acaa9b1f7230bb6b50 /src | |
parent | ab5b0ff3f3f096c86dfbf74123d0a9a61f9a0528 (diff) | |
download | jquery-48d71d0c3e53d1bb1688fc6c0593b008ced3403b.tar.gz jquery-48d71d0c3e53d1bb1688fc6c0593b008ced3403b.zip |
Fix #13514: Set selectedIndex to -1 when non-matching value is set on a select. Close gh-1191.
Diffstat (limited to 'src')
-rw-r--r-- | src/attributes.js | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/src/attributes.js b/src/attributes.js index 6902244ed..9a5276966 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -275,13 +275,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; |