
Question:
<input type="checkbox" name="feature1" value="0" />
If checked, value="1"; else value="0";
if ( $('input[name=feature1]').is(':checked') ) { $('input[name=feature1]').val('1'); }
I think the above jQuery may work, but its one-time check, how do I make it that everytime the checkbox is clicked/checked, value becomes 1, else 0?
Many thanks
Solution:1
$('input[name=feature1]').click(function(){ if ($(this).is(':checked') ) { $(this).val('1'); } else { $(this).val('0'); } });
or
$('input[name=feature1]').click(function(){ if (this.checked) { $(this).val('1'); } else { $(this).val('0'); } });
or
much better
$('input[name=feature1]').click(function(){ $(this).val(this.checked ? 1:0); });
and just as I thought there is still a much better way..
$('input[name=feature1]').click(function(){ this.value = this.checked ? 1:0; });â
Solution:2
I'd give the input an ID, just to make it a bit easier to write your jQuery - for the purposes of this answer I'll pretend we've duplicated the name.
jQuery("#feature1").click(function() { if (this.checked) { jQuery(this).val(1); } else { jQuery(this).val(0); } });
Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
EmoticonEmoticon