From 046071a41f33008db7c3489c99a5077a2de69d93 Mon Sep 17 00:00:00 2001 From: Todd Wolfson Date: Mon, 1 Feb 2016 11:56:47 -0600 Subject: [PATCH] Added default value for checkboxes/radios --- lib/api/attributes.js | 7 +++++++ test/api/attributes.js | 8 ++++++++ test/fixtures.js | 2 ++ 3 files changed, 17 insertions(+) diff --git a/lib/api/attributes.js b/lib/api/attributes.js index a4947bdd97..c7884bd35e 100644 --- a/lib/api/attributes.js +++ b/lib/api/attributes.js @@ -44,6 +44,13 @@ var getAttr = function(elem, name) { if (elem.name === 'option' && name === 'value') { return $.text(elem.children); } + + // Mimic DOM with default value for radios/checkboxes + if (elem.name === 'input' && + (elem.attribs.type === 'radio' || elem.attribs.type === 'checkbox') && + name === 'value') { + return 'on'; + } }; var setAttr = function(el, name, value) { diff --git a/test/api/attributes.js b/test/api/attributes.js index a5b3f1f3d0..c23a1ff4b7 100644 --- a/test/api/attributes.js +++ b/test/api/attributes.js @@ -399,10 +399,18 @@ describe('$(...)', function() { var val = $('input[name="checkbox_off"]').val(); expect(val).to.equal('off'); }); + it('.val(): on valueless checkbox should get value', function() { + var val = $('input[name="checkbox_valueless"]').val(); + expect(val).to.equal('on'); + }); it('.val(): on radio should get value', function() { var val = $('input[type="radio"]').val(); expect(val).to.equal('off'); }); + it('.val(): on valueless radio should get value', function() { + var val = $('input[name="radio_valueless"]').val(); + expect(val).to.equal('on'); + }); it('.val(): on multiple select should get an array of values', function() { var val = $('select#multi').val(); expect(val).to.eql(['2', '3']); diff --git a/test/fixtures.js b/test/fixtures.js index d73df80ff1..b2b127d888 100644 --- a/test/fixtures.js +++ b/test/fixtures.js @@ -46,8 +46,10 @@ exports.inputs = [ '', '', '', + '', '', '', + '', '', '' ].join('');