How to check a radio button with jQuery?

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

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

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

    Try this.

    To check Radio button using Value use this.

    $('input[name=type][value=2]').attr('checked', true); 
    

    Or

    $('input[name=type][value=2]').attr('checked', 'checked');
    

    Or

    $('input[name=type][value=2]').prop('checked', 'checked');
    

    To check Radio button using ID use this.

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

    Or

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

    The $.prop way is better:

    $(document).ready(function () {                            
        $("#radio_1").prop('checked', true);        
    });
    

    and you can test it like the following:

    $(document).ready(function () {                            
        $("#radio_1, #radio_2", "#radio_3").change(function () {
            if ($("#radio_1").is(":checked")) {
                $('#div1').show();
            }
            else if ($("#radio_2").is(":checked")) {
                $('#div2').show();
            }
            else 
                $('#div3').show();
        });        
    });
    
    0 讨论(0)
  • 2020-11-22 09:10
    function rbcitiSelction(e) {
         debugger
        $('#trpersonalemail').hide();
        $('#trcitiemail').show();
    }
    
    function rbpersSelction(e) {
        var personalEmail = $(e).val();
        $('#trpersonalemail').show();
        $('#trcitiemail').hide();
    }
    
    $(function() {  
        $("#citiEmail").prop("checked", true)
    });
    
    0 讨论(0)
  • 2020-11-22 09:11

    If property name does not work don't forget that id still exists. This answer is for people who wants to target the id here how you do.

    $('input[id=element_id][value=element_value]').prop("checked",true);
    

    Because property name does not work for me. Make sure you don't surround id and name with double/single quotations.

    Cheers!

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

    I've just have a similar problem, a simple solution is to just use:

    .click()
    

    Any other solution will work if you refresh radio after calling function.

    0 讨论(0)
  • 2020-11-22 09:13
    $("input[name=inputname]:radio").click(function() {
        if($(this).attr("value")=="yes") {
            $(".inputclassname").show();
        }
        if($(this).attr("value")=="no") {
            $(".inputclassname").hide();
        }
    });
    
    0 讨论(0)
提交回复
热议问题