六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 96|回复: 0

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

[复制链接]

升级  50%

5

主题

5

主题

5

主题

童生

Rank: 1

积分
25
 楼主| 发表于 2013-2-7 19:13:28 | 显示全部楼层 |阅读模式
第一种 定义一个表示是否已获得焦点的变量,是一种比较优雅的方法

 
<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>
 



 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表