开发者

Unselect group of radio buttons using jQuery

I'm using jQuery and I have a group of ra开发者_如何学JAVAdio buttons all with the same name, but different value properties.

For example:

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

I want to make it so they are all unselected. Current state of my page has one of them clicked. How do I do this?


$("input:radio[name='thename']").each(function(i) {
       this.checked = false;
});

not sure why the jquery prop doesn't work and this does...


As of jQuery 1.6, $("radio").prop("checked", false); is the suggested method.


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

Using jQuery's prop method can change properties of elements (checked, selected, ect.).


This is simple and works for me.

Try this one:

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

Try this one:

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


The answer posted by @matzahboy worked perfectly.

Tried other ways but this one worked the best:

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


Try the following code:

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


This is the simple and generic answer (I believe): $("input[name=NAME_OF_YOUR_RADIO_GROUP]").prop("checked",false);

For this specific question, I would use:

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

Hope this helps


it worked for me;

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


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);
});

This allows you to double click the radios to reset them.


To unselect all the radios of a group called "namegroup", try this:

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


`

$('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>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜