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;
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • 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++){
                    is_checked = true;
                alert("One or more questions must be filled out");
                return false;

    tested by:

    <form onsubmit="return validateForm()" method="post" action="./">
            echo <<<EOD
            <input type="radio" name="a{$x}" value="1">
            <input type="radio" name="a{$x}" value="-1">
        <input type="submit" value="submit">
    点赞 评论 复制链接分享
  • 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++)
    点赞 评论 复制链接分享