douchong4730 2013-11-05 21:21
浏览 15
已采纳

Javascript和单选按钮 - 没有做任何事情

This one is bound to be simple (I hope) something I am overlooking. I have a set of 50 questions that are asked pulled from a table and put into a form for answering. I want to check to make sure they have all been answered (required). When the user hits submit, none of the alert boxes (even the debugging boxes) appear. What am I dong wrong here?

First, the PHP:

     echo 'Please complete ALL 50 questions, then press the "SUBMIT" button at the bottom of the page.';
     $query = "SELECT * 
        FROM `starriskquestions` 
        ORDER BY `QuestionNum` ASC"; 
 $results = $pdo->query($query);
 echo '<form name="submitra" action="riskassessmenttest.php" onsubmit="return validateForm()" method="post">';
  while ($row = $results->fetch()) {
  echo '<br>' .$row["QuestionNum"] . ') ' . $row["Question"] . '<br>
            <input type="radio" name="a'.$row["QuestionNum"].'" value="1" /> Yes ---- 
            <input type="radio" name="a'.$row["QuestionNum"].'" value="-1" /> No<br><br>';
  } 
  echo "<br> ARE YOU SURE YOU ANSWERED ALL 50 QUESTIONS??? <br> If so, click the ";
  echo "submit buton below <br>";
  echo '<input type="hidden" name="testid" value="'.$testid.'">';
  echo '<input type="submit" name="submittestanswers" value="submit">';
  echo ' </form>';

Then the Javascript

    function validateForm()
{
    for (var answerloop=1; <=50; answerloop++)
    {
        var answernum = '"'+ "a" + answerloop + '"';
        alert (answerloop);
        var x=document.getElementByName(answernum).value;
        alert ("This is the variable X: " + x);

        if (x!=="1" || x!=="-1")
         {
            alert(" One or more questions must be filled out");
             return false;
         }
    }
}
  • 写回答

3条回答 默认 最新

  • dongzituo5530 2013-11-05 21:31
    关注

    1, incorrect second argument in for loop

    2, document.getElementByName() should be document.getElementsByName()

    function validateForm(){
        for (var answerloop=1; answerloop<=50; answerloop++){
            var name = 'a' + answerloop;
            var names=document.getElementsByName(name);
            var is_checked = false;
    
            for(var i=0;i<names.length;i++){
                if(names[i].checked){
                    is_checked = true;
                }
            }
            if(!is_checked){
                alert("One or more questions must be filled out");
                return false;
            }
    
        }
    }
    

    tested by:

    <form onsubmit="return validateForm()" method="post" action="./">
        <?php
        for($x=1;$x<=50;$x++){
            echo <<<EOD
            <input type="radio" name="a{$x}" value="1">
            <input type="radio" name="a{$x}" value="-1">
    EOD;
        }?>
        <input type="submit" value="submit">
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?