ITeye博客

两种解决用户名与密码都为空时焦点的问题

孙健坤 · 2009-11-15 15:15 · JavaScript · Markdown

第一种 定义一个表示是否已获得焦点的变量,是一种比较优雅的方法

 

<html>

  <head>

    <title>focus</title>

    <script type="text/javascript">

           function checkForm(myForm) {

              var msg = "";

              var setFocused = false; //是否已获得焦点

              if(myForm.account.value == "") {

                  msg += "帐号不能为空!\n";

                  myForm.account.focus();

                  setFocused = true;

              }

 

              if(myForm.password.value == "") {

                  msg += "密码不能为空!\n";

                  if(!setFocused) { //如果账号已获得了焦点 则密码不获得焦点

                     myForm.password.focus();

                     setFocused = true;

                  }

              }

             

              if(msg != "") {

                  alert(msg);

                  return false;

              }

             

              return true;

             

           }

       </script>

  </head>

 

  <body>

    <form action="" method="post" onsubmit="return checkForm(this);">

    <table>

        <tr>

              <td>

                  用户帐号

              </td>

              <td>

                  <input type="text" style="width:150px" name="account">

              </td>

           </tr>

           <tr>

              <td>

                  用户密码

              </td>

              <td>

                  <input type="password" style="width:150px" name="password">

              </td>

           </tr>

           <tr>

              <td colspan="2" align="center">

                  <input type="submit" value="登录">

                  <input type="reset" value="清空">

              </td>

           </tr>

    </table>

    </form>

  </body>

</html>

 

第二种 列出所有的情况,是一种比较笨拙的方法,只有两个字段还好,要是字段多了判断起来就麻烦了。

<html>

  <head>

    <title>focus</title>

    <script type="text/javascript">

           function checkForm(myForm) {

              var msg = "";

              if(myForm.account.value == "") {

                  msg += "帐号不能为空!\n";

                  myForm.account.focus();

              }

 

              if(myForm.account.value != "" && myForm.password.value == "") {

                  msg += "密码不能为空!\n";

                  myForm.password.focus();

              }

             

              if(myForm.account.value == "" && myForm.password.value == "") {

                  msg += "帐号不能为空!\n 密码不能为空!\n";

                  myForm.account.focus();

              }

             

              if(msg != "") {

                  alert(msg);

                  return false;

              }

              return true;

           }

       </script>

  </head>

 

  <body>

    <form action="" method="post" onsubmit="return checkForm(this);">

    <table>

        <tr>

              <td>

                  用户帐号

              </td>

              <td>

                  <input type="text" style="width:150px" name="account">

              </td>

           </tr>

           <tr>

              <td>

                  用户密码

              </td>

              <td>

                  <input type="password" style="width:150px" name="password">

              </td>

           </tr>

           <tr>

              <td colspan="2" align="center">

                  <input type="submit" value="登录">

                  <input type="reset" value="清空">

              </td>

           </tr>

    </table>

    </form>

  </body>

</html>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。