Skip to content

Commit 046071a

Browse files
committedFeb 1, 2016
Added default value for checkboxes/radios
1 parent c3ec1cd commit 046071a

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed
 

‎lib/api/attributes.js

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ var getAttr = function(elem, name) {
4444
if (elem.name === 'option' && name === 'value') {
4545
return $.text(elem.children);
4646
}
47+
48+
// Mimic DOM with default value for radios/checkboxes
49+
if (elem.name === 'input' &&
50+
(elem.attribs.type === 'radio' || elem.attribs.type === 'checkbox') &&
51+
name === 'value') {
52+
return 'on';
53+
}
4754
};
4855

4956
var setAttr = function(el, name, value) {

‎test/api/attributes.js

+8
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,18 @@ describe('$(...)', function() {
399399
var val = $('input[name="checkbox_off"]').val();
400400
expect(val).to.equal('off');
401401
});
402+
it('.val(): on valueless checkbox should get value', function() {
403+
var val = $('input[name="checkbox_valueless"]').val();
404+
expect(val).to.equal('on');
405+
});
402406
it('.val(): on radio should get value', function() {
403407
var val = $('input[type="radio"]').val();
404408
expect(val).to.equal('off');
405409
});
410+
it('.val(): on valueless radio should get value', function() {
411+
var val = $('input[name="radio_valueless"]').val();
412+
expect(val).to.equal('on');
413+
});
406414
it('.val(): on multiple select should get an array of values', function() {
407415
var val = $('select#multi').val();
408416
expect(val).to.eql(['2', '3']);

‎test/fixtures.js

+2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ exports.inputs = [
4646
'<select id="one-nested"><option>Option not selected</option><option selected>Option <span>selected</span></option></select>',
4747
'<input type="text" value="input_text" />',
4848
'<input type="checkbox" name="checkbox_off" value="off" /><input type="checkbox" name="checkbox_on" value="on" checked />',
49+
'<input type="checkbox" name="checkbox_valueless" />',
4950
'<input type="radio" value="off" name="radio" /><input type="radio" name="radio" value="on" checked />',
5051
'<input type="radio" value="off" name="radio[brackets]" /><input type="radio" name="radio[brackets]" value="on" checked />',
52+
'<input type="radio" name="radio_valueless" />',
5153
'<select id="multi" multiple><option value="1">1</option><option value="2" selected>2</option><option value="3" selected>3</option><option value="4">4</option></select>',
5254
'<select id="multi-valueless" multiple><option>1</option><option selected>2</option><option selected>3</option><option>4</option></select>'
5355
].join('');

0 commit comments

Comments
 (0)
Please sign in to comment.