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

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

提交回复
热议问题