Skip to content

Commit 425d17d

Browse files
gibson042dmethvin
authored andcommitted
Fix #12583: Don't ignore disabled property of select-one, close jquerygh-932.
1 parent da3ff3a commit 425d17d

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/attributes.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -225,26 +225,25 @@ jQuery.extend({
225225
},
226226
select: {
227227
get: function( elem ) {
228-
var value, i, max, option,
229-
index = elem.selectedIndex,
230-
values = [],
228+
var value, option,
231229
options = elem.options,
232-
one = elem.type === "select-one";
233-
234-
// Nothing was selected
235-
if ( index < 0 ) {
236-
return null;
237-
}
230+
index = elem.selectedIndex,
231+
one = elem.type === "select-one" || index < 0,
232+
values = one ? null : [],
233+
max = one ? index + 1 : options.length,
234+
i = index < 0 ?
235+
max :
236+
one ? index : 0;
238237

239238
// Loop through all the selected options
240-
i = one ? index : 0;
241-
max = one ? index + 1 : options.length;
242239
for ( ; i < max; i++ ) {
243240
option = options[ i ];
244241

245-
// Don't return options that are disabled or in a disabled optgroup
246-
if ( option.selected && (jQuery.support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null) &&
247-
(!option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" )) ) {
242+
// oldIE doesn't update selected after form reset (#2551)
243+
if ( ( option.selected || i === index ) &&
244+
// Don't return options that are disabled or in a disabled optgroup
245+
( jQuery.support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null ) &&
246+
( !option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" ) ) ) {
248247

249248
// Get the specific value for the option
250249
value = jQuery( option ).val();
@@ -259,11 +258,6 @@ jQuery.extend({
259258
}
260259
}
261260

262-
// Fixes Bug #2551 -- select.val() broken in IE after form.reset()
263-
if ( one && !values.length && options.length ) {
264-
return jQuery( options[ index ] ).val();
265-
}
266-
267261
return values;
268262
},
269263

test/unit/attributes.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ test("removeProp(String)", function() {
718718
});
719719

720720
test("val()", function() {
721-
expect( 20 + ( jQuery.fn.serialize ? 6 : 0 ) );
721+
expect( 21 + ( jQuery.fn.serialize ? 6 : 0 ) );
722722

723723
document.getElementById("text1").value = "bla";
724724
equal( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
@@ -761,6 +761,12 @@ test("val()", function() {
761761
jQuery("#select5").val(3);
762762
equal( jQuery("#select5").val(), "3", "Check value on ambiguous select." );
763763

764+
strictEqual(
765+
jQuery("<select name='select12584' id='select12584'><option value='1' disabled='disabled'>1</option></select>").val(),
766+
null,
767+
"Select-one with only option disabled (#12584)"
768+
);
769+
764770
if ( jQuery.fn.serialize ) {
765771
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");
766772

0 commit comments

Comments
 (0)