From b03416954d8033e641bd658fb18631699db6cc51 Mon Sep 17 00:00:00 2001 From: Colin Snover Date: Fri, 8 Oct 2010 22:48:06 -0500 Subject: [PATCH] Coerce all array values to strings before comparison in val(). Fixes bug #7123. --- src/attributes.js | 4 ++++ test/unit/attributes.js | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/attributes.js b/src/attributes.js index ec4841bd3..147c353f4 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -215,6 +215,10 @@ jQuery.fn.extend({ val = ""; } else if ( typeof val === "number" ) { val += ""; + } else if ( jQuery.isArray(val) ) { + val = jQuery.map(val, function (value) { + return value == null ? "" : value + ""; + }); } if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) { diff --git a/test/unit/attributes.js b/test/unit/attributes.js index a483195a4..c6007aacc 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -409,7 +409,19 @@ test("val(String/Number)", function() { test("val(Function)", function() { testVal(functionReturningObj); -}) +}); + +test( "val(Array of Numbers) (Bug #7123)", function() { + expect(4); + jQuery('#form').append(''); + var elements = jQuery('input[name=arrayTest]').val([ 1, 2 ]); + ok( elements[0].checked, "First element was checked" ); + ok( elements[1].checked, "Second element was checked" ); + ok( !elements[2].checked, "Third element was unchecked" ); + ok( !elements[3].checked, "Fourth element remained unchecked" ); + + elements.remove(); +}); test("val(Function) with incoming value", function() { expect(10); -- 2.39.5