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 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?