请检查以下控制台输出,注意第一个输入已选中属性并已选中:
你可以看到我有 2 个 radio input
s, 1 是 checked
, 但是既不是选择器也不是 prop
也不is
为它工作。如果我手动单击其中一个 radio ,代码将正常工作。这是我的 HTML:
<div class="col-sm-6">
Type:
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default active">
<input type="radio" name="generator-type" id="opt-type-passphrase" value="0" autocomplete="off" checked>
Passphrase
</label>
<label class="btn btn-default">
<input type="radio" name="generator-type" id="opt-type-password" value="1" autocomplete="off">
Password
</label>
</div>
</div>
为什么当我没有点击任何单选按钮时页面加载时代码不工作?有办法解决吗?
编辑:发现问题,我在页面加载中有这段代码:
$("input[name='generator-type'][value=" + this.Options.Type + "]").prop("checked", "");
正确的解决方案是使用:
.prop("checked", true)
但是谁能解释一下它是如何工作的?为什么会有checked
属性,但是 prop
函数还是假的?
最佳答案
尝试 .is(':checked')
而不是 .is('checked')
。请检查这个Fiddle .
关于javascript - jQuery :checked not working with initial (HTML attribute) radio input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38110399/