点击jquery添加属性'checked' [英] Add attribute 'checked' on click jquery

查看:158
本文介绍了点击jquery添加属性'checked'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图弄清楚如何在点击时将checked属性添加到复选框。我想这样做的原因是,如果我检查一个复选框;我可以将我的本地存储保存为html,所以当页面刷新时通知复选框被选中。截至目前,如果我检查它,它会使父母退色,但如果我保存并重新加载它会保持褪色但复选框未选中。

I've been trying to figure out how to add the attribute "checked" to a checkbox on click. The reason I want to do this is so if I check off a checkbox; I can have my local storage save that as the html so when the page refreshes it notices the checkbox is checked. As of right now if I check it off, it fades the parent, but if I save and reload it stays faded but the checkbox is unchecked.

我试过了$(本).attr( '检查');但它似乎不想添加已检查。

I've tried doing $(this).attr('checked'); but it does not seem to want to add checked.

编辑:
阅读评论后,我似乎并不清楚。
我的默认输入标签是:

After reading comments it seems i wasn't being clear. My default input tag is:

<input type="checkbox" class="done">

当我点击复选框时,我需要将它添加到顶部,它将已选中添加到最后那。例如:

I need it top be so when I click the checkbox, it adds "checked" to the end of that. Ex:

<input type="checkbox" class="done" checked>

我需要这样做,所以当我将html保存到本地存储时,加载时,它将复选框呈现为已选中。

I need it to do this so when I save the html to local storage, when it loads, it renders the checkbox as checked.

$(".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'); //This line
}else{

$(this).parent().fadeTo('slow', 1);
$(this).removeAttr('checked');
}
});


推荐答案

似乎这是其中罕见的场合之一使用属性实际上是合适的。 jQuery的 attr()方法不会对你有所帮助,因为在大多数情况下(包括这个)它实际上设置了一个属性,而不是属性,这使得它的名字选择看起来有些愚蠢。 [更新:自jQuery 1.6.1起,情况略有改变]

It seems this is one of the rare occasions on which use of an attribute is actually appropriate. jQuery's attr() method will not help you because in most cases (including this) it actually sets a property, not an attribute, making the choice of its name look somewhat foolish. [UPDATE: Since jQuery 1.6.1, the situation has changed slightly]

IE有一些问题DOM setAttribute 方法但在这种情况下应该没问题:

IE has some problems with the DOM setAttribute method but in this case it should be fine:

this.setAttribute("checked", "checked");

在IE中,这实际上总是会勾选复选框。在其他浏览器中,如果用户已经选中并取消选中该复选框,则设置该属性将不会有明显效果。因此,如果你想保证复选框被选中并且被选中属性,你需要设置被选中属性:

In IE, this will always actually make the checkbox checked. In other browsers, if the user has already checked and unchecked the checkbox, setting the attribute will have no visible effect. Therefore, if you want to guarantee the checkbox is checked as well as having the checked attribute, you need to set the checked property as well:

this.setAttribute("checked", "checked");
this.checked = true;

要取消选中复选框并删除属性,请执行以下操作:

To uncheck the checkbox and remove the attribute, do the following:

this.setAttribute("checked", ""); // For IE
this.removeAttribute("checked"); // For other browsers
this.checked = false;

这篇关于点击jquery添加属性'checked'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆