Using JQuery to check if no radio button in a group has been checked

后端 未结 8 1641
太阳男子
太阳男子 2020-12-12 15:42

I\'m sitting with a problem, I need to check with JQuery if no radio button within a radio button group has been checked, so that I can give the users an javascript error if

相关标签:
8条回答
  • 2020-12-12 15:55

    I am using this much simple

    HTML

    <label class="radio"><input id="job1" type="radio" name="job" value="1" checked>New Job</label>
    <label class="radio"><input id="job2" type="radio" name="job" value="2">Updating Job</label>
    
    
    <button type="button" class="btn btn-primary" onclick="save();">Save</button>
    

    SCRIPT

     $('#save').on('click', function(e) {
        if (job1.checked)
            {
                  alert("New Job"); 
            }
    if (job2.checked)
            {
                alert("Updating Job");
            }
    

    }

    0 讨论(0)
  • 2020-12-12 15:56
    if (!$("input[name='html_elements']:checked").val()) {
       alert('Nothing is checked!');
    }
    else {
      alert('One of the radio buttons is checked!');
    }
    
    0 讨论(0)
  • 2020-12-12 16:00

    I'm using

    $("input:radio[name='html_radio']").is(":checked")
    

    And will return FALSE if all the items in the radiogroup are unchecked and TRUE if an item is checked.

    0 讨论(0)
  • 2020-12-12 16:03

    You could do something like this:

    var radio_buttons = $("input[name='html_elements']");
    if( radio_buttons.filter(':checked').length == 0){
      // None checked
    } else {
      // If you need to use the result you can do so without
      // another (costly) jQuery selector call:
      var val = radio_buttons.val();
    }
    
    0 讨论(0)
  • 2020-12-12 16:04
    if ($("input[name='html_elements']:checked").size()==0) {
       alert('Nothing is checked!');
    }
    else {
      alert('One of the radio buttons is checked!');
    }
    
    0 讨论(0)
  • 2020-12-12 16:07
    var len = $('#your_form_id input:radio:checked').length;
          if (!len) {
            alert("None checked");
          };
          alert("checked: "+ len);
    
    0 讨论(0)
提交回复
热议问题