JavaScript radio button confirmation

一个人想着一个人 提交于 2019-11-28 13:05:33

Set your radio buttons' values like this:

<td><input type="radio" id="examtype" name="examtype" value="GCSE" /> : GCSE<br />
<td><input type="radio" id="examtype" name="examtype" value="A2" /> : A2<br />
<td><input type="radio" id="examtype" name="examtype" value="AS"/> : AS<br />

Then use this script in your function to validate form or make it a function and call it from your function validateForm:

var checked = null;
var inputs = document.getElementsByName('examtype');
for (var i = 0; i < inputs.length; i++) {
          if (inputs[i].checked) {
           checked = inputs[i];
           break;
   }
}
if(checked==null)
{
    alert('Please choose an option');
    return false;
}
else{
    return confirm('You have chosen '+checked.value+' is this correct?');
}
xXRaider
add this to your code#

    function confirmation() {

    for (var i=0; i < document.ExamEntry.examtype.length; i++)
       {
       if (document.ExamEntry.examtype[i].checked)
          {
            var answer = confirm(document.ExamEntry.examtype[i].value)

        if (answer){
            document.ExamEntry.examtype[i].checked = true;

        }
        else{
            document.ExamEntry.examtype[i].checked = false;

        }
          }
    }

    }

Then add to you radio button's

( value="a2" )
( value="a1" )
( value="G1" )

And also add to your radio button's (onclick="return confirmation();") remember to put this in all of you radio buttons soz if this doesn't work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!