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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化