开发者

Event by clicking Checkbox

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜