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

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

 

<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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值