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

前端 未结 28 2439
面向向阳花
面向向阳花 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:39

    HTML:

    
    
    
    
    
    

    JAVAScript:

    var options = document.getElementsByName("calculation");
    
    for (var i = 0; i < options.length; i++) {
        if (options[i].checked) {
            // do whatever you want with the checked radio
            var calc = options[i].value;
            }
        }
        if(typeof calc == "undefined"){
            document.getElementById("result").innerHTML = " select the operation you want to perform";
            return false;
    }
    

提交回复
热议问题