简体   繁体   中英

How can I pass unchecked checkbox to cookie in jQuery?

I can pass the checked value just fine. But when I uncheck it, the value is null and doesn't overwrite the cookie. What do you recommend?

$("input[name=box]").change(function(e) {
    $.cookie("autoplay", $(e.target).val());
    window.location.reload();
});

and the html

<input type="checkbox" name="box" value="1">

SOLUTION:

$.cookie("autoplay", this.checked);

I just used this to track true or false.

使用三元运算符可捕获所有虚假值:

$(e.target).val() ? 1 : 0

this.checked is fastest if the value isn't necessary. As referenced here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM