在点击事件中,添加属性'checked'。使用jquery。

8 浏览
0 Comments

在点击事件中,添加属性'checked'。使用jquery。

我一直在尝试找出如何在点击时向复选框添加属性“checked”。我之所以这样做,是因为如果我选中了复选框,我希望将其保存到本地存储中,以便在页面刷新时能够识别复选框是否被选中。目前,如果我选中复选框,它会使父元素淡出,但如果我保存并重新加载页面,父元素仍然是淡出的,但复选框没有被选中。

我尝试使用$(this).attr('checked');,但似乎无法添加checked属性。

编辑:

阅读评论后,我意识到自己表达不清楚。

我默认的input标签是:


我希望当我点击复选框时,它在末尾添加"checked"。例如:


我需要这样做是因为当我将html保存到本地存储中并加载时,它会渲染为选中的复选框。

$(".done").live("click", function(){
if($(this).parent().find('.editor').is(':visible') ) {
var editvar = $(this).parent().find('input[name="tester"]').val();
$(this).parent().find('.editor').fadeOut('slow');
$(this).parent().find('.content').text(editvar);
$(this).parent().find('.content').fadeIn('slow');
}
if ($(this).is(':checked')) {
$(this).parent().fadeTo('slow', 0.5);
$(this).attr('checked'); //这一行
}else{
$(this).parent().fadeTo('slow', 1);
$(this).removeAttr('checked');
}
});

0