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

前端 未结 28 2471
面向向阳花
面向向阳花 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 01:48

    An example:

    if (!checkRadioArray(document.ExamEntry.level)) { 
        msg+="What is your level of entry? \n"; 
        document.getElementById('entry').style.color="red"; 
        result = false; 
    } 
    
    if(msg==""){ 
        return result;  
    } 
    else{ 
        alert(msg) 
        return result;
    } 
    
    function Radio() { 
        var level = radio.value; 
        alert("Your level is: " + level + " \nIf this is not the level your taking then please choose another.") 
    } 
    
    function checkRadioArray(radioButtons) { 
        for(var r=0;r < radioButtons.length; r++) { 
            if (radioButtons[r].checked) { 
                return true; 
            } 
        } 
        return false; 
    } 
    
    0 讨论(0)
  • 2020-11-22 01:52

    http://www.somacon.com/p143.php/

    function getCheckedValue(radioObj) {
        if(!radioObj)
            return "";
        var radioLength = radioObj.length;
        if(radioLength == undefined)
            if(radioObj.checked)
                return radioObj.value;
            else
                return "";
        for(var i = 0; i < radioLength; i++) {
            if(radioObj[i].checked) {
                return radioObj[i].value;
            }
        }
        return "";
    }
    
    0 讨论(0)
  • 2020-11-22 01:54

    With JQuery, another way to check the current status of the radio buttons is to get the attribute 'checked'.

    For Example:

    <input type="radio" name="gender_male" value="Male" />
    <input type="radio" name="gender_female" value="Female" />
    

    In this case you can check the buttons using:

    if ($("#gender_male").attr("checked") == true) {
    ...
    }
    
    0 讨论(0)
  • 2020-11-22 01:54

    This is also working, avoiding to call for an element id but calling it using as an array element.

    The following code is based on the fact that an array, named as the radiobuttons group, is composed by radiobuttons elements in the same order as they where declared in the html document:

    if(!document.yourformname.yourradioname[0].checked 
       && !document.yourformname.yourradioname[1].checked){
        alert('is this working for all?');
        return false;
    }
    
    0 讨论(0)
  • 2020-11-22 01:55

    A vanilla JavaScript way

    var radios = document.getElementsByTagName('input');
    var value;
    for (var i = 0; i < radios.length; i++) {
        if (radios[i].type === 'radio' && radios[i].checked) {
            // get value, set checked flag or do whatever you need to
            value = radios[i].value;       
        }
    }
    
    0 讨论(0)
  • 2020-11-22 01:55

    If you want vanilla JavaScript, don't want to clutter your markup by adding IDs on each radio button, and only care about modern browsers, the following functional approach is a little more tasteful to me than a for loop:

    <form id="myForm">
    <label>Who will be left?
      <label><input type="radio" name="output" value="knight" />Kurgan</label>
      <label><input type="radio" name="output" value="highlander" checked />Connor</label>
    </label>
    </form>
    
    <script>
    function getSelectedRadioValue (formElement, radioName) {
        return ([].slice.call(formElement[radioName]).filter(function (radio) {
            return radio.checked;
        }).pop() || {}).value;
    }
    
    var formEl = document.getElementById('myForm');
    alert(
       getSelectedRadioValue(formEl, 'output') // 'highlander'
    )
    </script>
    

    If neither is checked, it will return undefined (though you could change the line above to return something else, e.g., to get false returned, you could change the relevant line above to: }).pop() || {value:false}).value;).

    There is also the forward-looking polyfill approach since the RadioNodeList interface should make it easy to just use a value property on the list of form child radio elements (found in the above code as formElement[radioName]), but that has its own problems: How to polyfill RadioNodeList?

    0 讨论(0)
提交回复
热议问题