无法使用jQuery以编程方式"检查"单选按钮

der*_*lwc 1 forms jquery radio-button

我有一组单选按钮,并且想要使按钮本身和附加文本都可以点击(以提高可用性).

这应该非常简单,但由于某种原因,当从jQuery触发时,单选按钮的状态不会更新.会发生什么是checked属性将出现在DOM中,但单选按钮似乎仍然未经检查(可视).

注意:当我从控制台运行代码时,它可以正常工作,但是当我的函数正常触发它时,它不会更新.此外,此代码非常适合取消检查组中的其他单选按钮...所以我很困惑为什么它不能在另一个方向工作.

这是我的标记:

<div class="radio-input">
    <input type="radio" name="ad_category" id="cat_standard">
    <span class="inline-label">Standard</span>
</div>
<div class="radio-input">
    <input type="radio" name="ad_category" id="cat_reward">
    <span class="inline-label">Reward</span>
</div>
Run Code Online (Sandbox Code Playgroud)

和jQuery:

$(".radio-input").click( function() {

  if (!$(this).children("input:radio").attr("checked")) {

    $(this).siblings(".radio-input").children("input:radio").removeAttr("checked");

    $(this).children("input:radio").attr("checked","checked");

  }
});
Run Code Online (Sandbox Code Playgroud)

对于记录,调用click()元素会产生相同的非结果,即使它从控制台触发时工作正常.此外,双方.prop.attr产生相同的结果(在控制台工作,在正常流动不工作).

Nie*_*sol 7

有什么不对:

<label><input type="radio" name="ad_category"> Standard</label>
Run Code Online (Sandbox Code Playgroud)

<label>毕竟这就是标签的用途......

无论如何,要回答实际问题,checked 属性checked 属性之间存在差异.这种差异的一个例子是文本字段:input.value是当前值,并input.getAttribute("value")保持默认值.