开发者

How can I pass unchecked checkbox to cookie in jQuery?

开发者 https://www.devze.com 2023-04-03 22:22 出处:网络
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?

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

<inpu开发者_开发百科t type="checkbox" name="box" value="1">

SOLUTION:

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

I just used this to track true or false.


Use the ternary operator to catch all falsy values :

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


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

0

精彩评论

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

关注公众号