Update value based on :checked status
<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, e开发者_如何学Pythonlse 0?
Many thanks
$('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;
});
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);
}
});
精彩评论