I see lots of people on stackoverflow, across the internet, and my colleagues use values in their checkboxes that are not 1 or 0 (ex. <input type='checkbox' value='check'/>
), but my thought is that essentially a checkbox will always be a yes/no answer.
Currently I use a jQuery script I wrote to standardize across my applications that looks like:
;(function($){
$(document).ready(function() {
var selector = 'input[type="checkbox"]';
var c = 'checked';
$.each($(selector),function(){
var $el,value,attr;
$el = $(this);
value = $el.val();
attr = $el.attr(c);
if(value == 1 || typeof(attr) !== 'undefined')
{
$el.attr(c,c);
$el.prop(c,true);
$el.val(1);
}
else{
$el.removeAttr(c);
$el.prop(c,false);
$el.val(0);
}
});
$(selector).change(function(e){
var $el, checked = null;
$el = $(this);
checked = $el.prop(c);
if (checked) {
$el.attr(c,c);
$el.val(1);
} else {
$el.removeAttr(c);
$el.val(0);
}
});
});
})(jQuery);
So that I can handle any of these situations:
<input type='checkbox' value='1'/>
<input type='checkbox' checked/>
<input type='checkbox' checked='checked'/>
<input type='checkbox' value='1' disabled/>
<input type='checkbox' checked disabled/>
<input type='checkbox' checked='checked' disabled/>
<input type='checkbox' value='0'/>
<input type='checkbox'/>
<input type='checkbox' value='0' disabled/>
<input type='checkbox' disabled/>
But it does not account for any values of the checkboxes that are not 0 or 1 or having the attribute 'checked'.
My question is: Is there ever a good use-case for using a non 0/null or 1 value (especially when working with a relational database)?