猿问

单选按钮没有切换

我有单选按钮和其中之一:checked,它们具有相同的名称。当我单击另一个单选按钮时,它们都被选中。


我的代码是:


<input name="a" type="radio">

<input name="a "type="radio" checked>

JS


$("input[type='radio']").each(function () {

    if ($(this).is(":checked")) {

    $(this).addClass("some");

   }

 });

CSS


  .some {

      background-color:#06c!important;

  }

我的方法是首先检查我的输入是否是:checked,如果是,则放置一些带有背景颜色的 css 类。我实现了这一点,接下来我想要删除的是:checked当用户单击单选按钮或任何其他(更好的)想法时。现在两个单选按钮都被选中。有人可以帮我解决这个问题吗?


小提琴:


https://jsfiddle.net/0ns58xcq/


翻翻过去那场雪
浏览 199回答 3
3回答

扬帆大鱼

看看你的标记:<input name="a" type="radio"><input name="a "type="radio" checked>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ----- There is a space删除空间,它应该可以工作。<input name="a" type="radio"><input name="a" type="radio" checked>

汪汪一只猫

'a'与'a '.Thename属性包含CDATA 不同。它可以或多或少是你喜欢的任何东西。(您不应该包含前导或尾随,white space因为用户代理可以忽略它,但中间的空白是可以的)。我应该使用什么作为名称属性$("input[type='radio']").each(function () {&nbsp; &nbsp; if ($(this).is(":checked")) {&nbsp; &nbsp; $(this).addClass("some");&nbsp; &nbsp;}&nbsp;});&nbsp; .some {&nbsp; &nbsp; &nbsp; background-color:#06c!important;&nbsp; }<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script><input name="a" type="radio"><input name="a"type="radio" checked>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答