我想设置一个cookie来收听一个复选框(true / false),这样就会改变我设置cookie的页面代码下面的cookie状态
$(document).ready(function ()
{
$('#ContentPlaceHolder1_chkSD').change(function setCookie() {
if ($('#ContentPlaceHolder1_chkSD').is('checked')) {
document.cookie(setCookie, true);
// console.log('cooooooookie = true');
}
else {
// console.log('kooouuuuuukiiiie = false');
document.cookie(setCookie, false);
}
})
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) +
((exdays == null) ? "" : ("; expires=" + exdate.toUTCString()));
document.cookie = c_name + "=" + c_value;
}
这不起作用,我得到0x800a138a - JavaScript运行时错误:一旦我点击复选框就会出现功能
答案 0 :(得分:1)
通过传递预期参数(cookie名称,值和过期),确保正确调用setCookie
函数:
if ($('#ContentPlaceHolder1_chkSD').is('checked')) {
setCookie("cookie_name", "true", null);
} else {
setCookie("cookie_name", "false", null);
}