开发者

Event by clicking Checkbox

开发者 https://www.devze.com 2023-03-31 08:14 出处:网络
How do, when click a checkbox, in var check=\'\'; will be added - okay, and if click again,okay开发者_StackOverflow will be removed.

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 = '';})
0

精彩评论

暂无评论...
验证码 换一张
取 消