代码之家  ›  专栏  ›  技术社区  ›  Mbrouwer88

检查是否选中单选按钮jquery[重复]

  •  36
  • Mbrouwer88  · 技术社区  · 11 年前

    我有这个HTML:

    <input type="radio" name="test" id="test" value="1"><br>
    <input type="radio" name="test" id="test" value="2"><br>
    <input type="radio" name="test" id="test" value="3"><br>
    
    <input type="button" onclick="CreateJobDo">
    

    当页面加载时,不会选中任何一个。我想使用jQuery来检查按下按钮时是否选中了其中一个单选按钮。

    我有这个:

    function CreateJobDo(){
        if ($("input[@name=test]:checked").val() == 'undefined') {
    
            // go on with script
    
        } else {
    
            // NOTHING IS CHECKED
        }
    }
    

    但这并不奏效。如何使其工作?

    5 回复  |  直到 7 年前
        1
  •  79
  •   Ray    11 年前

    首先,只有一个 id="test"

    其次,试试这个:

    if ($('[name="test"]').is(':checked'))
    
        2
  •  50
  •   Mohamed Habib    11 年前

    试试这个

    if($('input:radio:checked').length > 0){
    // go on with script
     }else{
        // NOTHING IS CHECKED
     }
    
        3
  •  17
  •   RestingRobot    11 年前

    类似这样的内容:

     $("input[name=test]").is(":checked");
    

    使用jQuery is()函数应该可以工作。

        4
  •  11
  •   talha2k    11 年前
    if($("input:radio[name=test]").is(":checked")){
      //Code to append goes here
    }
    
        5
  •  3
  •   Ray    11 年前
      $('#submit_button').click(function() {
        if (!$("input[@name='name']:checked").val()) {
           alert('Nothing is checked!');
            return false;
        }
        else {
          alert('One of the radio buttons is checked!');
        }
      });