Skip to content

Commit 2c4b208

Browse files
dmethvinjeresig
authored andcommitted
Don't have .val() return selected-but-disabled options, or selected options inside a disabled optgroup. Doesn't change the .val() returned for a disabled select. Fixes #3240, adapted from Nathan Hammond's patch there.
1 parent 700ff05 commit 2c4b208

File tree

6 files changed

+31
-12
lines changed

6 files changed

+31
-12
lines changed

src/attributes.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,10 @@ jQuery.fn.extend({
163163
for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
164164
var option = options[ i ];
165165

166-
if ( option.selected ) {
167-
// Get the specifc value for the option
166+
// Don't return options that are disabled or in a disabled optgroup
167+
if ( option.selected && !option.disabled &&
168+
(!option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" )) ) {
169+
// Get the specific value for the option
168170
value = jQuery(option).val();
169171

170172
// We don't need an array for one selects

test/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ <h2 id="qunit-userAgent"></h2>
114114
<option id="option3d" value="3">3</option>
115115
<option id="option3e">no value</option>
116116
</select>
117+
<select name="select4" id="select4" multiple="multiple">
118+
<optgroup disabled="disabled">
119+
<option id="option4a" class="emptyopt" value="">Nothing</option>
120+
<option id="option4b" disabled="disabled" selected="selected" value="1">1</option>
121+
<option id="option4c" selected="selected" value="2">2</option>
122+
</optgroup>
123+
<option selected="selected" disabled="disabled" id="option4d" value="3">3</option>
124+
<option id="option4e">no value</option>
125+
</select>
117126

118127
<object id="object1" codebase="stupid">
119128
<param name="p1" value="x1" />

test/unit/attributes.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ test("removeAttr(String)", function() {
302302
});
303303

304304
test("val()", function() {
305-
expect(17);
305+
expect(20);
306306

307307
document.getElementById('text1').value = "bla";
308308
equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
@@ -329,6 +329,14 @@ test("val()", function() {
329329
jQuery('#select3').val("");
330330
same( jQuery('#select3').val(), [''], 'Call val() on a multiple="multiple" select' );
331331

332+
same( jQuery('#select4').val(), [], 'Call val() on multiple="multiple" select with all disabled options' );
333+
334+
jQuery('#select4 optgroup').add('#select4 > [disabled]').attr('disabled', false);
335+
same( jQuery('#select4').val(), ['2', '3'], 'Call val() on multiple="multiple" select with some disabled options' );
336+
337+
jQuery('#select4').attr('disabled', true);
338+
same( jQuery('#select4').val(), ['2', '3'], 'Call val() on disabled multiple="multiple" select' );
339+
332340
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");
333341

334342
same( checks.serialize(), "", "Get unchecked values." );

test/unit/event.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ test("bind(), iframes", function() {
255255
});
256256

257257
test("bind(), trigger change on select", function() {
258-
expect(3);
258+
expect(4);
259259
var counter = 0;
260260
function selectOnChange(event) {
261261
equals( event.data, counter++, "Event.data is not a global event object" );

test/unit/selector.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test("element", function() {
2121
same( jQuery("p", jQuery("div")).get(), q("firstp","ap","sndp","en","sap","first"), "Finding elements with a context." );
2222
same( jQuery("div").find("p").get(), q("firstp","ap","sndp","en","sap","first"), "Finding elements with a context." );
2323

24-
same( jQuery("#form").find("select").get(), q("select1","select2","select3"), "Finding selects with a context." );
24+
same( jQuery("#form").find("select").get(), q("select1","select2","select3","select4"), "Finding selects with a context." );
2525

2626
ok( jQuery("#length").length, '&lt;input name="length"&gt; cannot be found under IE, see #945' );
2727
ok( jQuery("#lengthtest input").length, '&lt;input name="length"&gt; cannot be found under IE, see #945' );
@@ -338,7 +338,7 @@ test("pseudo - :not", function() {
338338
expect(24);
339339
t( "Not", "a.blog:not(.link)", ["mark"] );
340340

341-
t( "Not - multiple", "#form option:not(:contains(Nothing),#option1b,:selected)", ["option1c", "option1d", "option2b", "option2c", "option3d", "option3e"] );
341+
t( "Not - multiple", "#form option:not(:contains(Nothing),#option1b,:selected)", ["option1c", "option1d", "option2b", "option2c", "option3d", "option3e", "option4e"] );
342342
t( "Not - recursive", "#form option:not(:not(:selected))[id^='option3']", [ "option3b", "option3c"] );
343343

344344
t( ":not() failing interior", "p:not(.foo)", ["firstp","ap","sndp","en","sap","first"] );
@@ -360,8 +360,8 @@ test("pseudo - :not", function() {
360360
t( "No element not selector", ".container div:not(.excluded) div", [] );
361361

362362
t( ":not() Existing attribute", "#form select:not([multiple])", ["select1", "select2"]);
363-
t( ":not() Equals attribute", "#form select:not([name=select1])", ["select2", "select3"]);
364-
t( ":not() Equals quoted attribute", "#form select:not([name='select1'])", ["select2", "select3"]);
363+
t( ":not() Equals attribute", "#form select:not([name=select1])", ["select2", "select3", "select4"]);
364+
t( ":not() Equals quoted attribute", "#form select:not([name='select1'])", ["select2", "select3", "select4"]);
365365

366366
t( ":not() Multiple Class", "#foo a:not(.blog)", ["yahoo","anchor2"] );
367367
t( ":not() Multiple Class", "#foo a:not(.link)", ["yahoo","anchor2"] );
@@ -427,13 +427,13 @@ test("pseudo - visibility", function() {
427427
test("pseudo - form", function() {
428428
expect(8);
429429

430-
t( "Form element :input", "#form :input", ["text1", "text2", "radio1", "radio2", "check1", "check2", "hidden1", "hidden2", "name", "search", "button", "area1", "select1", "select2", "select3"] );
430+
t( "Form element :input", "#form :input", ["text1", "text2", "radio1", "radio2", "check1", "check2", "hidden1", "hidden2", "name", "search", "button", "area1", "select1", "select2", "select3", "select4"] );
431431
t( "Form element :radio", "#form :radio", ["radio1", "radio2"] );
432432
t( "Form element :checkbox", "#form :checkbox", ["check1", "check2"] );
433433
t( "Form element :text", "#form :text:not(#search)", ["text1", "text2", "hidden2", "name"] );
434434
t( "Form element :radio:checked", "#form :radio:checked", ["radio2"] );
435435
t( "Form element :checkbox:checked", "#form :checkbox:checked", ["check1"] );
436436
t( "Form element :radio:checked, :checkbox:checked", "#form :radio:checked, #form :checkbox:checked", ["radio2", "check1"] );
437437

438-
t( "Selected Option Element", "#form option:selected", ["option1a","option2d","option3b","option3c"] );
438+
t( "Selected Option Element", "#form option:selected", ["option1a","option2d","option3b","option3c","option4b","option4c","option4d"] );
439439
});

test/unit/traversing.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ test("not(Selector)", function() {
152152
equals( jQuery("#main > p#ap > a").not("#google").length, 2, "not('selector')" );
153153
same( jQuery("p").not(".result").get(), q("firstp", "ap", "sndp", "en", "sap", "first"), "not('.class')" );
154154
same( jQuery("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
155-
same( jQuery("#form option").not("option.emptyopt:contains('Nothing'),[selected],[value='1']").get(), q("option1c", "option1d", "option2c", "option3d", "option3e" ), "not('complex selector')");
155+
same( jQuery("#form option").not("option.emptyopt:contains('Nothing'),[selected],[value='1']").get(), q("option1c", "option1d", "option2c", "option3d", "option3e", "option4e" ), "not('complex selector')");
156156

157157
same( jQuery('#ap *').not('code').get(), q("google", "groups", "anchor1", "mark"), "not('tag selector')" );
158158
same( jQuery('#ap *').not('code, #mark').get(), q("google", "groups", "anchor1"), "not('tag, ID selector')" );
@@ -163,7 +163,7 @@ test("not(Element)", function() {
163163
expect(1);
164164

165165
var selects = jQuery("#form select");
166-
same( selects.not( selects[1] ).get(), q("select1", "select3"), "filter out DOM element");
166+
same( selects.not( selects[1] ).get(), q("select1", "select3", "select4"), "filter out DOM element");
167167
});
168168

169169
test("not(Function)", function() {

0 commit comments

Comments
 (0)