【发布时间】:2015-06-08 08:41:02
【问题描述】:
我希望当用户选择多个输入时,jQuery 应该设置一个 cookie,然后将我的类添加到特定的 li,其中 cookie 的值等于 li 的 data-id。在这件事上我需要你的帮助。
$(document).ready(function(){
$("input").change(function() {
if ($(this).is(':checked')) {
createCookie(this.name, this.value, 1000);
$("li").filter('[data-id=' + this.value + ']').addClass('prelight');
} else {
$("li").filter('[data-id=' + this.value + ']').removeClass('prelight');
eraseCookie(this.name, this.value);
}
});
if (readCookie(this.name)) {
$("li").filter('[data-id=' + this.value + ']').addClass('prelight');
$('input').filter('[name=' + this.name + ']').attr('checked', 'checked');
}
});
更多详情请看这个http://jsfiddle.net/4pf8hsgf/3/
【问题讨论】:
-
您需要将
if (readCookie(this.name))包装成$("input")。否则它只会匹配到文档(如果有的话) -
我这样做了,但仍然无法正常工作。为什么我的脚本不能正常工作!!!这是怎么回事!!!