Skip to content

Commit

Permalink
Made sure that the .val() logic for setting radios and checkboxes was…
Browse files Browse the repository at this point in the history
… correct. Fixes #5698.
  • Loading branch information
jeresig committed Dec 22, 2009
1 parent 261b7ef commit f298cce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/attributes.js
Expand Up @@ -158,7 +158,7 @@ jQuery.fn.extend({
}

if ( jQuery.isArray(val) && /radio|checkbox/.test( this.type ) ) {
this.checked = jQuery.inArray(this.value || this.name, val) >= 0;
this.checked = jQuery.inArray( this.value, val ) >= 0;

} else if ( jQuery.nodeName( this, "select" ) ) {
var values = jQuery.makeArray(val);
Expand Down
18 changes: 17 additions & 1 deletion test/unit/manipulation.js
Expand Up @@ -619,7 +619,7 @@ test("clone() on XML nodes", function() {
}

test("val()", function() {
expect(11);
expect(15);

document.getElementById('text1').value = "bla";
equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
Expand All @@ -646,6 +646,22 @@ test("val()", function() {
jQuery('#select3').val("");
same( jQuery('#select3').val(), [''], 'Call val() on a multiple="multiple" select' );

var checks = jQuery("<input type='checkbox' name='test' value='1'/>").appendTo("#form")
.add( jQuery("<input type='checkbox' name='test' value='2'/>").appendTo("#form") )
.add( jQuery("<input type='checkbox' name='test' value=''/>").appendTo("#form") );

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

checks.val([ "2" ]);
same( checks.serialize(), "test=2", "Get a single checked value." );

checks.val([ "1", "" ]);
same( checks.serialize(), "test=1&test=", "Get multiple checked values." );

checks.val([ "", "2" ]);
same( checks.serialize(), "test=2&test=", "Get multiple checked values." );

checks.remove();
});

var testVal = function(valueObj) {
Expand Down

0 comments on commit f298cce

Please sign in to comment.