【问题标题】:How to check radio button is checked using JQuery?如何使用 JQuery 检查单选按钮?
【发布时间】:2012-02-02 06:06:25
【问题描述】:

我在一组中有两个单选按钮,我想使用 JQuery 检查单选按钮是否被选中,如何?

【问题讨论】:

标签: javascript jquery radio-button


【解决方案1】:

您只需检查属性即可。

if( $("input[name='radioButtonName']").prop('checked') ){
    //implement your logic
  }else{
    //do something else as radio not checked   
  }

【讨论】:

    【解决方案2】:

    试试这个:

    var count =0;
    
    $('input[name="radioGroup"]').each(function(){
      if (this.checked)
        {
          count++;
        }
    });
    

    如果选中任何单选按钮,您将得到 1

    【讨论】:

      【解决方案3】:

      jQuery 3.3.1

      if (typeof $("input[name='yourRadioName']:checked").val() === "undefined") {
          alert('is not selected');
      }else{
          alert('is selected');
      }
      

      【讨论】:

        【解决方案4】:

        单选按钮是,

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

        检查点击,

        $('.radioButtons').click(function(){
            if($("#radio_1")[0].checked){
               //logic here
            }
        });
        

        【讨论】:

          【解决方案5】:

          进一步回答一些问题 - 如果您执行以下操作,您可以检查单选组中的任何元素是否已被检查:

          if ($('input[name="yourRadioNames"]:checked').val()){(选中)或if (!$('input[name="yourRadioNames"]:checked').val()){(未选中)

          【讨论】:

            【解决方案6】:

            这是最佳做法

            $("input[name='radioGroup']:checked").val()
            

            【讨论】:

              【解决方案7】:

              也看看这个:

              $(document).ready(function() { 
                if($("input:radio[name='yourRadioGroupName'][value='yourvalue']").is(":checked")) { 
                    //its checked 
                } 
              });
              

              【讨论】:

                【解决方案8】:
                //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 } });

                【讨论】:

                • 描述您的代码的实际作用会有所帮助(对于提问者 - 而不是我)
                【解决方案9】:

                给定一组单选按钮:

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

                您可以使用jQuery测试是否检查了特定的一个,如下所示:

                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"))
                

                可以通过如下方式获取组中当前选中的值:

                $("input[name='radioGroup']:checked").val()
                

                【讨论】:

                  猜你喜欢
                  • 2011-08-05
                  • 1970-01-01
                  • 1970-01-01
                  • 2019-05-29
                  • 1970-01-01
                  • 2016-05-08
                  • 2016-08-17
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多