douchong4730
douchong4730
2013-11-05 21:21

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 dongzituo5530 8年前

    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>
    
    点赞 评论 复制链接分享
  • doushi8599 doushi8599 8年前

    Your for loop is missing answerloop in the second param

    for (var answerloop=1; <=50; answerloop++)
                          ^ 
    

    change to

    for (var answerloop=1; answerloop<=50; answerloop++)
    
    点赞 评论 复制链接分享
  • dongyu4863 dongyu4863 8年前

    I think this is wrong:

    for (var answerloop=1; <=50; answerloop++)
    

    Change it to this:

    for (var answerloop=1; answerloop <=50; answerloop++)
    
    点赞 评论 复制链接分享

为你推荐