How can I check whether a radio button is selected with JavaScript?

前端 未结 28 2437
面向向阳花
面向向阳花 2020-11-22 00:58

I have two radio buttons within an HTML form. A dialog box appears when one of the fields is null. How can I check whether a radio button is selected?

相关标签:
28条回答
  • 2020-11-22 02:00

    Return all checked element in the radio button

      Array.from(document.getElementsByClassName("className")).filter(x=>x['checked']);
    
    0 讨论(0)
  • 2020-11-22 02:01

    You can use this simple script. You may have multiple radio buttons with same names and different values.

    var checked_gender = document.querySelector('input[name = "gender"]:checked');
    
    if(checked_gender != null){  //Test if something was checked
    alert(checked_gender.value); //Alert the value of the checked.
    } else {
    alert('Nothing checked'); //Alert, nothing was checked.
    }
    
    0 讨论(0)
  • 2020-11-22 02:01

    The scripts in this page helped me come up with the script below, which I think is more complete and universal. Basically it will validate any number of radio buttons in a form, meaning that it will make sure that a radio option has been selected for each one of the different radio groups within the form. e.g in the test form below:

       <form id="FormID">
    
        Yes <input type="radio" name="test1" value="Yes">
        No <input type="radio" name="test1" value="No">
    
        <br><br>
    
        Yes <input type="radio" name="test2" value="Yes">
        No <input type="radio" name="test2" value="No">
    
       <input type="submit" onclick="return RadioValidator();">
    

    The RadioValidator script will make sure that an answer has been given for both 'test1' and 'test2' before it submits. You can have as many radio groups in the form, and it will ignore any other form elements. All missing radio answers will show inside a single alert popup. Here it goes, I hope it helps people. Any bug fixings or helpful modifications welcome :)

    <SCRIPT LANGUAGE="JAVASCRIPT">
    function RadioValidator()
    {
        var ShowAlert = '';
        var AllFormElements = window.document.getElementById("FormID").elements;
        for (i = 0; i < AllFormElements.length; i++) 
        {
            if (AllFormElements[i].type == 'radio') 
            {
                var ThisRadio = AllFormElements[i].name;
                var ThisChecked = 'No';
                var AllRadioOptions = document.getElementsByName(ThisRadio);
                for (x = 0; x < AllRadioOptions.length; x++)
                {
                     if (AllRadioOptions[x].checked && ThisChecked == 'No')
                     {
                         ThisChecked = 'Yes';
                         break;
                     } 
                }   
                var AlreadySearched = ShowAlert.indexOf(ThisRadio);
                if (ThisChecked == 'No' && AlreadySearched == -1)
                {
                ShowAlert = ShowAlert + ThisRadio + ' radio button must be answered\n';
                }     
            }
        }
        if (ShowAlert != '')
        {
        alert(ShowAlert);
        return false;
        }
        else
        {
        return true;
        }
    }
    </SCRIPT>
    
    0 讨论(0)
  • 2020-11-22 02:01

    This would be valid for radio buttons sharing the same name, no JQuery needed.

    var x = Array.prototype.filter.call(document.getElementsByName('checkThing'), function(x) { return x.checked })[0];
    

    If we are talking about checkboxes and we want a list with the checkboxes checked sharing a name:

    var x = Array.prototype.filter.call(document.getElementsByName('checkThing'), function(x) { return x.checked });
    
    0 讨论(0)
提交回复
热议问题