使用 jQuery 取消选择一组单选按钮

问题描述 投票:0回答:11

我正在使用 jQuery,我有一组单选按钮,它们的名称都相同,但值属性不同。

例如:

<input type = "radio" name = "thename" value="1"></input>
<input type = "radio" name = "thename" value="2"></input>
<input type = "radio" name = "thename" value="3"></input>

我想把它们全部取消选中。我的页面的当前状态已单击其中之一。我该怎么做?

jquery radio-button
11个回答
10
投票
$("input:radio[name='thename']").each(function(i) {
       this.checked = false;
});

不确定为什么 jquery 道具不起作用而这确实...


9
投票

从 jQuery 1.6 开始,

$("radio").prop("checked", false);
是建议的方法。


5
投票

尝试使用这个:

$('input[type="radio"]').prop('checked', false);

使用 jQuery 的 prop 方法可以更改元素的属性(选中、选中等)。


3
投票

这很简单,对我有用。

试试这个:

$('input:radio[name="gender"]').attr('checked',false);

试试这个:

$('input[name="gender"]').prop('checked', false);

2
投票

@matzahboy 发布的答案非常有效。

尝试过其他方法,但这个方法效果最好:

$(input[name=thename]).removeAttr('checked');

1
投票

试试下面的代码:

$(input[name=thename]).removeAttr('checked');

1
投票

这是简单而通用的答案(我相信):

$("input[name=NAME_OF_YOUR_RADIO_GROUP]").prop("checked",false);

对于这个具体问题,我会使用:

$("input[name=thename]").prop("checked",false);

希望这有帮助


1
投票

对我有用;

$('input[name="radioName"]').attr('checked', false);

0
投票
function resetRadio(name) {
    $('#form input:radio[name=' + name + ']:checked').each(function () {
        var $this = $(this);
        $this.prop("checked", false);
    });
}

$('#form input:radio').on('dblclick', function () {
    var $this = $(this);
    var name = $this.prop('name');
    resetRadio(name);
});

这允许您双击收音机以重置它们。


0
投票

要取消选择名为“namegroup”的组的所有无线电,试试这个:

$("input[type=radio][name=namegroup]").prop("checked", false);

0
投票

`

$('input[name="radio-choices"]').change(function() {
    let currentValue =$(this).val();
    if (currentValue == 'choices1'){
      $("input[name='radio-test-name']").each(function(i) {
        this.checked = false;
        this.disabled = true;
      });
    } else {
      $("input[name='radio-test-name']").each(function(i) {
        this.disabled = false;
      });
    }
    
    })
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<title> Uncheck radio button
</title>
<body>
  <input type="radio" name="radio-choices" value="choices2" id="choices2" checked><label for="choices2">Can pick</label>
  <input type="radio" name="radio-choices" value="choices1" id="choices1"><label for="choices1">Uncheck and disabled</label>
  <br>
  <input type="radio" name="radio-test-name" id="test1"><label for="test1">TEST1</label>
  <input type="radio" name="radio-test-name" id="test2"><label for="test2">TEST2</label>
  <input type="radio" name="radio-test-name" id="test3"><label for="test3">TEST3</label>
</body>
</html>

© www.soinside.com 2019 - 2024. All rights reserved.