[英]Set radio button checked - label doesn't change
I'd like to remove and set a radio button checked.我想删除并设置一个单选按钮。 In the snippet it works like a charm but on localhost it doesn't.
在代码片段中,它就像一个魅力,但在 localhost 上却没有。 The input gets checked though but the label doesn't change.
虽然输入被检查,但标签不会改变。 If I set another radio button (eq #b) as checked, the label changes.
如果我将另一个单选按钮 (eq #b) 设置为选中状态,则标签会更改。
Here is the JSFiddle.这是 JSFiddle。
$('#a').attr('checked', false);
$('#a').attr('checked', true);
input:checked + label {
/* background-color, font-size, etc. */
}
use .prop
instead of .attr
, latter will result in html like `checked=false" not a removel of checked property.使用
.prop
而不是.attr
,后者将导致像 `checked=false" 这样的 html 而不是检查属性的删除。
Edit: The underlying problem is that OP used .attr
on two radios of the same name, .attr
does not ever remove the porperty checked.编辑:潜在的问题是 OP 在两个同名的收音机上使用了
.attr
, .attr
永远不会删除检查的属性。 That ends up giving you two radios which are checked, that is invalid.这最终会为您提供两个经过检查的无线电,这是无效的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.