checkbox value 0 or 1
Thanks worked as in
$('#custom7').on('change', function(){
this.value = this.checked ? 1 : 0;
// alert(this.value);
}).change();
link: http://jsfiddle.net/WhQaR/
It makes no much sense because unchecked checkboxes are not sent to the server as said in the comments, but if you need a value in the server you can use a hidden field:
HTML:
<input type="checkbox" value="1" id="custom7" checked="checked" />
<input type="hidden" value="1" id="hdncustom7" name="custom7" />
<label for="custom7">Email me more info?</label>
jQuery:
$('#custom7').on('change', function(){
$('#hdncustom7').val(this.checked ? 1 : 0);
});
Using this methods you will receive custom7
with 0 or 1 in the server
Don't forget about the bitwise XOR
operator:
$('input[type="checkbox"]').on('change', function(){
this.value ^= 1;
});
$('input[type="checkbox"]').on('change', function(){
this.value ^= 1;
console.log( this.value )
});
<label><input type="checkbox" name="accept" value="1" checked> I accept </label>
<label><input type="checkbox" name="accept" value="0"> Unchecked example</label>
<script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
Just to be a butt and offer a slightly shorter answer:
$('input[type="checkbox"]').change(function(){
this.value = (Number(this.checked));
});