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

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

    Give radio buttons, same name but different IDs.

    var verified1 = $('#SOME_ELEMENT1').val();
    var verified2 = $('#SOME_ELEMENT2').val();
    var final_answer = null;
    if( $('#SOME_ELEMENT1').attr('checked') == 'checked' ){
      //condition
      final_answer = verified1;
    }
    else
    {
      if($('#SOME_ELEMENT2').attr('checked') == 'checked'){
        //condition
        final_answer = verified2;
       }
       else
       {
         return false;
       }
    }
    

提交回复
热议问题