How do, when click a checkbox, in var check='';
will be added - okay
,
and if click again,okay开发者_StackOverflow
will be removed.
How it make?
var check = "";
$("#yourcheckbox").change(function() {
if($(this).is(":checked")) {
check = "okay";
}
else {
check = "";
}
});
This does exactly what you ask, but the solution posted by @Rikudo is better if you can live without check
having to contain "okay".
var check = false;
console.log(check);
$('#checkbox').change(function() {
check = $(this).is(':checked');
console.log(check);
});
Will set the variable to true
or false
depending on the state.
Example.
var check;
$('#checkbox').change(function() {this.checked ? check = 'okay' : check = '';})
精彩评论