最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

forms - Why does the JavaScript code stop execution after the for loop - Stack Overflow

programmeradmin0浏览0评论

This is my try at creating a registration page however after the for loop whatever follows does not execute and I'm at a loss as to why? Below is the javascript code and the form

<script type='text/javascript'>
myArray = ['username','email','password','password2'];
reply = ['User Name','Email','Password','Password Again'];
message = "Please Type you're ";
start = "<font color = red size='-1'>";
end = "</font>";
test = 10;
obj = validate();
alert (obj);
function validate(){

for (var i=0; i<=myArray.length; i++) {
               if (document.getElementById("in"+myArray[i]).value == "")
               {
                   document.getElementById(myArray[i]).innerHTML = start+message+reply[i]+end;
                   test = 30;
               }
                }
alert ("test");
}

<form action="echo.php" method="post" name="register" onsubmit="return validate();">
<table>
    <tr>
      <td width="185"><font size="2">First Name</font></td>
      <td width="499">
        <input id = "infirst_name" name="first_name" type="text" /><div id="first_name"></div></td>
    </tr>
    <tr>
      <td><font size="2">Last Name</font></td>
      <td><input id = "inlast_name" name="last_name" type="text" /><div id="last_name"></font></div></td>
    </tr>
    <tr>
      <td><font size="2">Username*</font></td>
      <td>
        <input id = "inusername" name="username" type="text" /><div id="username"></div></td>
    </tr>
    <tr>
      <td><font size="2">Email*</font></td>
      <td><input id = "inemail" name="email" type="text" /><div id="email"></div></td>
    </tr>
    <tr>
      <td><font size="2">Password*</font></td>
      <td><input id = "inpassword" name="password" type="password" /><div id="password"></div></td>
    </tr>
    <tr>
      <td><font size="2">Repeat Password*</font></td>
      <td>
        <input id = "inpassword2" name="password2" type="password" /><div id="password2"></div></td>
    </tr>
    <tr>
    <tr>
      <td><font size="2">I have read and Agree to the Terms and Conditions</font></td>
      <td>
        <input id = "incheckme" name="checkme" type="checkbox" /><div id="checkme"></div></td>
    </tr>
    <tr>
      <td><input type="submit" value="Register" /></td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>

This is my try at creating a registration page however after the for loop whatever follows does not execute and I'm at a loss as to why? Below is the javascript code and the form

<script type='text/javascript'>
myArray = ['username','email','password','password2'];
reply = ['User Name','Email','Password','Password Again'];
message = "Please Type you're ";
start = "<font color = red size='-1'>";
end = "</font>";
test = 10;
obj = validate();
alert (obj);
function validate(){

for (var i=0; i<=myArray.length; i++) {
               if (document.getElementById("in"+myArray[i]).value == "")
               {
                   document.getElementById(myArray[i]).innerHTML = start+message+reply[i]+end;
                   test = 30;
               }
                }
alert ("test");
}

<form action="echo.php" method="post" name="register" onsubmit="return validate();">
<table>
    <tr>
      <td width="185"><font size="2">First Name</font></td>
      <td width="499">
        <input id = "infirst_name" name="first_name" type="text" /><div id="first_name"></div></td>
    </tr>
    <tr>
      <td><font size="2">Last Name</font></td>
      <td><input id = "inlast_name" name="last_name" type="text" /><div id="last_name"></font></div></td>
    </tr>
    <tr>
      <td><font size="2">Username*</font></td>
      <td>
        <input id = "inusername" name="username" type="text" /><div id="username"></div></td>
    </tr>
    <tr>
      <td><font size="2">Email*</font></td>
      <td><input id = "inemail" name="email" type="text" /><div id="email"></div></td>
    </tr>
    <tr>
      <td><font size="2">Password*</font></td>
      <td><input id = "inpassword" name="password" type="password" /><div id="password"></div></td>
    </tr>
    <tr>
      <td><font size="2">Repeat Password*</font></td>
      <td>
        <input id = "inpassword2" name="password2" type="password" /><div id="password2"></div></td>
    </tr>
    <tr>
    <tr>
      <td><font size="2">I have read and Agree to the Terms and Conditions</font></td>
      <td>
        <input id = "incheckme" name="checkme" type="checkbox" /><div id="checkme"></div></td>
    </tr>
    <tr>
      <td><input type="submit" value="Register" /></td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
Share edited Apr 10, 2012 at 11:16 Niet the Dark Absol 325k85 gold badges474 silver badges600 bronze badges asked Apr 10, 2012 at 11:13 user1154128user1154128 1
  • Have you checked the javascript error console? – user1233508 Commented Apr 10, 2012 at 11:15
Add a ment  | 

3 Answers 3

Reset to default 7

Try fixing your loop:

for (var i=0; i < myArray.length; i++) {

JavaScript will stop executing anything if it encounters an uncaught error. In your case it seems that you've looped past the last element (and tried to set innerHTML on an undefined object).

Most likely because you iterate past the end of the array, but reference it like there's something there.

See JS console output: perhaps it says that you have an error. i falls out of myArray length: change the loop condition from "i<="to "i<" and you'll be fine.

发布评论

评论列表(0)

  1. 暂无评论