How to reset radiobuttons in jQuery so that none is checked

后端 未结 13 670
一个人的身影
一个人的身影 2020-11-29 16:35

I have radio buttons in HTML like this:


    1
    

        
相关标签:
13条回答
  • 2020-11-29 17:15

    I know this is old and that this is a little off topic, but supposing you wanted to uncheck only specific radio buttons in a collection:

    $("#go").click(function(){
        $("input[name='correctAnswer']").each(function(){
          if($(this).val() !== "1"){
            $(this).prop("checked",false);
          }
        });
      });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <input id="radio1" type="radio" name="correctAnswer" value="1">1</input>
    <input id="radio2" type="radio" name="correctAnswer" value="2">2</input>
    <input id="radio3" type="radio" name="correctAnswer" value="3">3</input>
    <input id="radio4" type="radio" name="correctAnswer" value="4">4</input>
    <input type="button" id="go" value="go">

    And if you are dealing with a radiobutton list, you can use the :checked selector to get just the one you want.

    $("#go").click(function(){
      $("input[name='correctAnswer']:checked").prop("checked",false);
    });
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <input id="radio1" type="radio" name="correctAnswer" value="1">1</input>
    <input id="radio2" type="radio" name="correctAnswer" value="2">2</input>
    <input id="radio3" type="radio" name="correctAnswer" value="3">3</input>
    <input id="radio4" type="radio" name="correctAnswer" value="4">4</input>
    <input type="button" id="go" value="go">
    
    0 讨论(0)
  • 2020-11-29 17:18

    Although prop changed the checked status but change also show that in the form.

    $('input[name="correctAnswer"]').prop('checked', false).change();
    
    0 讨论(0)
  • 2020-11-29 17:24

    Your problem is that the attribute selector doesn't start with a @.

    Try this:

    $('input[name="correctAnswer"]').attr('checked', false);
    
    0 讨论(0)
  • 2020-11-29 17:25

    If you want to clear all radio buttons in the DOM:

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

    0 讨论(0)
  • 2020-11-29 17:27
    <div id="radio">
    <input type="radio" id="radio1" name="radio"/><label for="radio1">Bar Chart</label>
    <input type="radio" id="radio2" name="radio"/><label for="radio2">Pie Chart</label>
    <input type="radio" id="radio3" name="radio"/><label for="radio3">Datapoint Chart</label>
    </div>
    
    $('#radio input').removeAttr('checked');
    // Refresh the jQuery UI buttonset.                  
    $( "#radio" ).buttonset('refresh');
    
    0 讨论(0)
  • 2020-11-29 17:30

    Finally after a lot of tests, I think the most convenient and efficient way to preset is:

    var presetValue = "black";
    $("input[name=correctAnswer]").filter("[value=" + presetValue + "]").prop("checked",true);
    $("input[name=correctAnswer]").button( "refresh" );//JQuery UI only
    

    The refresh is required with the JQueryUI object.

    Retrieving the value is easy :

    alert($('input[name=correctAnswer]:checked').val())
    

    Tested with JQuery 1.6.1, JQuery UI 1.8.

    0 讨论(0)
提交回复
热议问题