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

前端 未结 28 2785
面向向阳花
面向向阳花 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; 
    } 
    

提交回复
热议问题