]> source.dussan.org Git - jquery.git/commitdiff
Fix #12583: Don't ignore disabled property of select-one, close gh-932.
authorRichard Gibson <richard.gibson@gmail.com>
Fri, 21 Sep 2012 15:53:03 +0000 (11:53 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Thu, 4 Oct 2012 01:14:08 +0000 (21:14 -0400)
src/attributes.js
test/unit/attributes.js

index cde7e4732f805cffb0a3eb63608c60123f43001c..76aed29960d4447bcb795db67a164dfa8b77ebd6 100644 (file)
@@ -225,26 +225,25 @@ jQuery.extend({
                },
                select: {
                        get: function( elem ) {
-                               var value, i, max, option,
-                                       index = elem.selectedIndex,
-                                       values = [],
+                               var value, option,
                                        options = elem.options,
-                                       one = elem.type === "select-one";
-
-                               // Nothing was selected
-                               if ( index < 0 ) {
-                                       return null;
-                               }
+                                       index = elem.selectedIndex,
+                                       one = elem.type === "select-one" || index < 0,
+                                       values = one ? null : [],
+                                       max = one ? index + 1 : options.length,
+                                       i = index < 0 ?
+                                               max :
+                                               one ? index : 0;
 
                                // Loop through all the selected options
-                               i = one ? index : 0;
-                               max = one ? index + 1 : options.length;
                                for ( ; i < max; i++ ) {
                                        option = options[ i ];
 
-                                       // Don't return options that are disabled or in a disabled optgroup
-                                       if ( option.selected && (jQuery.support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null) &&
-                                                       (!option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" )) ) {
+                                       // oldIE doesn't update selected after form reset (#2551)
+                                       if ( ( option.selected || i === index ) &&
+                                                       // Don't return options that are disabled or in a disabled optgroup
+                                                       ( jQuery.support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null ) &&
+                                                       ( !option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" ) ) ) {
 
                                                // Get the specific value for the option
                                                value = jQuery( option ).val();
@@ -259,11 +258,6 @@ jQuery.extend({
                                        }
                                }
 
-                               // Fixes Bug #2551 -- select.val() broken in IE after form.reset()
-                               if ( one && !values.length && options.length ) {
-                                       return jQuery( options[ index ] ).val();
-                               }
-
                                return values;
                        },
 
index 40c81a37cc8624b48869105fe4c9b6da1b969082..9c3b55c2180c0ad372613887a49fb8a68e50c428 100644 (file)
@@ -718,7 +718,7 @@ test("removeProp(String)", function() {
 });
 
 test("val()", function() {
-       expect( 20 + ( jQuery.fn.serialize ? 6 : 0 ) );
+       expect( 21 + ( jQuery.fn.serialize ? 6 : 0 ) );
 
        document.getElementById("text1").value = "bla";
        equal( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
@@ -761,6 +761,12 @@ test("val()", function() {
        jQuery("#select5").val(3);
        equal( jQuery("#select5").val(), "3", "Check value on ambiguous select." );
 
+       strictEqual(
+               jQuery("<select name='select12584' id='select12584'><option value='1' disabled='disabled'>1</option></select>").val(),
+               null,
+               "Select-one with only option disabled (#12584)"
+       );
+
        if ( jQuery.fn.serialize ) {
                var checks = jQuery("<input type='checkbox' name='test' value='1'/><input type='checkbox' name='test' value='2'/><input type='checkbox' name='test' value=''/><input type='checkbox' name='test'/>").appendTo("#form");