]> source.dussan.org Git - jquery.git/commitdiff
Fix #13514: Set selectedIndex to -1 when non-matching value is set on a select. Close...
authorruado1987 <ruado1987@gmail.com>
Sun, 3 Mar 2013 15:51:04 +0000 (23:51 +0800)
committerRichard Gibson <richard.gibson@gmail.com>
Fri, 22 Mar 2013 16:46:13 +0000 (12:46 -0400)
src/attributes.js
test/unit/attributes.js

index 6902244edf645a6bd678d6f26526d46e52eb0287..9a527696655fa4d7ebdc9ec6960f00b66d1d102a 100644 (file)
@@ -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;
index ccd1f4a99d02657df1c969ef612abe79916306d3..f0ca137e612880a3e16b369bc69f4da32d6db904 100644 (file)
@@ -850,6 +850,22 @@ test( "val()", function() {
        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") &&
                        "value" in document.createElement("progress") ) {