简体   繁体   English

jQuery removeAttr并添加属性

[英]Jquery removeAttr and add Attribute

I am trying to do something very simple. 我正在尝试做一些非常简单的事情。 I have 10 input fields and one of these has the checked attribute. 我有10个输入字段,其中之一具有checked属性。 I want to remove this attribute and apply it to another known input field. 我想删除此属性并将其应用于另一个已知的输入字段。

My code is as follows: 我的代码如下:

$(document).ready(function() {
    $('input.article-vote').removeAttr('checked');
    $('input#rating' + id).attr('checked', 'checked');
});

As can be seen I am remove the attribute by class and selecting the target by id. 可以看出,我按类删除属性,并按id选择目标。

When I run this code the checked attribute is removed but the target isn't applied. 当我运行此代码时,选中的属性被删除,但未应用目标。 If I remove the removeAttr line then the target has the attribute applied. 如果删除removeAttr行,则目标已应用属性。

How can I remove all and apply to a target of my choice? 如何删除所有内容并将其应用于自己选择的目标?

You don't want to remove the checked attribute, but rather set it to true or false. 您不想删除checked属性,而是将其设置为true或false。

$(document).ready(function() {
  $('input.article-vote').prop('checked', false);
  $('input#rating' + id).prop('checked', true);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM