开发者

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

<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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜