How to check radio button is checked using JQuery?

后端 未结 8 734
猫巷女王i
猫巷女王i 2020-12-24 07:20

I have two radio buttons in one group, I want to check the radio button is checked or not using JQuery, How ?

相关标签:
8条回答
  • 2020-12-24 07:21

    Taking some answers one step further - if you do the following you can check if any element within the radio group has been checked:

    if ($('input[name="yourRadioNames"]:checked').val()){ (checked) or if (!$('input[name="yourRadioNames"]:checked').val()){ (not checked)

    0 讨论(0)
  • 2020-12-24 07:33
    
    //the following code checks if your radio button having name like 'yourRadioName' 
    //is checked or not
    $(document).ready(function() {
      if($("input:radio[name='yourRadioName']").is(":checked")) {
          //its checked
      }
    });
    
    
    0 讨论(0)
  • 2020-12-24 07:35

    Check this one out, too:

    $(document).ready(function() { 
      if($("input:radio[name='yourRadioGroupName'][value='yourvalue']").is(":checked")) { 
          //its checked 
      } 
    });
    
    0 讨论(0)
  • 2020-12-24 07:41

    Given a group of radio buttons:

    <input type="radio" id="radio1" name="radioGroup" value="1">
    <input type="radio" id="radio2" name="radioGroup" value="2">
    

    You can test whether a specific one is checked using jQuery as follows:

    if ($("#radio1").prop("checked")) {
       // do something
    }
    
    // OR
    if ($("#radio1").is(":checked")) {
       // do something
    }
    
    // OR if you don't have ids set you can go by group name and value
    // (basically you need a selector that lets you specify the particular input)
    if ($("input[name='radioGroup'][value='1']").prop("checked"))
    

    You can get the value of the currently checked one in the group as follows:

    $("input[name='radioGroup']:checked").val()
    
    0 讨论(0)
  • 2020-12-24 07:41

    Radio buttons are,

    <input type="radio" id="radio_1" class="radioButtons" name="radioButton" value="1">
    <input type="radio" id="radio_2" class="radioButtons" name="radioButton" value="2">
    

    to check on click,

    $('.radioButtons').click(function(){
        if($("#radio_1")[0].checked){
           //logic here
        }
    });
    
    0 讨论(0)
  • 2020-12-24 07:41

    jQuery 3.3.1

    if (typeof $("input[name='yourRadioName']:checked").val() === "undefined") {
        alert('is not selected');
    }else{
        alert('is selected');
    }
    
    0 讨论(0)
提交回复
热议问题