JavaScript radio button confirmation

后端 未结 2 1557
别跟我提以往
别跟我提以往 2020-12-11 12:34

I am trying to add a function, or additional JavaScript to the existing function for a confirmation. I will need a confirmation box once the submit button is clicked saying,

相关标签:
2条回答
  • 2020-12-11 12:44
    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.

    0 讨论(0)
  • 2020-12-11 12:50

    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?');
    }
    
    0 讨论(0)
提交回复
热议问题