How to check a radio button with jQuery?

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

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

相关标签:
30条回答
  • 2020-11-22 09:04

    You have to do

    jQuery("#radio_1").attr('checked', 'checked');
    

    That's the HTML attribute

    0 讨论(0)
  • 2020-11-22 09:04

    Try this

    $(document).ready(function(){
        $("input[name='type']:radio").change(function(){
            if($(this).val() == '1')
            {
              // do something
            }
            else if($(this).val() == '2')
            {
              // do something
            }
            else if($(this).val() == '3')
            {
              // do something
            }
        });
    });
    
    0 讨论(0)
  • 2020-11-22 09:04

    In addition, you can check if the element is checked or not:

    if ($('.myCheckbox').attr('checked'))
    {
       //do others stuff
    }
    else
    {
       //do others stuff
    }
    

    You can checked for unchecked element:

    $('.myCheckbox').attr('checked',true) //Standards way
    

    You can also uncheck this way:

    $('.myCheckbox').removeAttr('checked')
    

    You can checked for radio button:

    For versions of jQuery equal or above (>=) 1.6, use:

    $("#radio_1").prop("checked", true);
    

    For versions prior to (<) 1.6, use:

    $("#radio_1").attr('checked', 'checked');
    
    0 讨论(0)
  • 2020-11-22 09:07

    For versions of jQuery equal or above (>=) 1.6, use:

    $("#radio_1").prop("checked", true);
    

    For versions prior to (<) 1.6, use:

    $("#radio_1").attr('checked', 'checked');
    

    Tip: You may also want to call click() or change() on the radio button afterwards. See comments for more info.

    0 讨论(0)
  • 2020-11-22 09:07

    I got some related example to be enhanced, how about if I want to add a new condition, lets say, if I want colour scheme to be hidden after I click on project Status value except Pavers and Paving Slabs.

    Example is in here:

    $(function () {
        $('#CostAnalysis input[type=radio]').click(function () {
            var value = $(this).val();
    
            if (value == "Supply & Lay") {
                $('#ul-suplay').empty();
                $('#ul-suplay').append('<fieldset data-role="controlgroup"> \
    

    http://jsfiddle.net/m7hg2p94/4/

    0 讨论(0)
  • 2020-11-22 09:07

    try this

     $("input:checked", "#radioButton").val()
    

    if checked returns True if not checked returns False

    jQuery v1.10.1
    
    0 讨论(0)
提交回复
热议问题