How to check a radio button with jQuery?

前端 未结 30 2078
独厮守ぢ
独厮守ぢ 2020-11-22 08:12

I try to check a radio button with jQuery. Here\'s my code:

相关标签:
30条回答
  • 2020-11-22 08:46

    Just in case anyone is trying to achieve this while using jQuery UI, you will also need to refresh the UI checkbox object to reflect the updated value:

    $("#option2").prop("checked", true); // Check id option2
    $("input[name='radio_options']").button("refresh"); // Refresh button set
    
    0 讨论(0)
  • 2020-11-22 08:47

    We should want to tell it is a radio button.So please try with following code.

    $("input[type='radio'][name='userRadionButtonName']").prop('checked', true);
    
    0 讨论(0)
  • 2020-11-22 08:47

    Try this with example

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form id="myForm">
    <input type="radio" name="radio" value="first"/> 1 <br/>
    <input type="radio" name="radio" value="second"/> 2 <br/>
    </form>
    
    
    <script>
    $(document).ready(function () {
        $('#myForm').on('click', function () {
            var value = $("[name=radio]:checked").val();
    
            alert(value);
        })
    });
    </script>
    
    0 讨论(0)
  • 2020-11-22 08:47

    Try this

    var isChecked = $("#radio_1")[0].checked;
    
    0 讨论(0)
  • 2020-11-22 08:49

    One more function prop() that is added in jQuery 1.6, that serves the same purpose.

    $("#radio_1").prop("checked", true); 
    
    0 讨论(0)
  • 2020-11-22 08:49

    Try This:

    $("input[name=type]").val(['1']);
    

    http://jsfiddle.net/nwo706xw/

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