if(document.form1.txtusername.value=="")                   
      {
        alert("您的用户名忘记填写了!");  
        document.form1.txtpassword.focus();       
      }       
      else if(document.all.txtpassword.value=="")      
      {
         alert("您的密码忘记填写了!");
         document.form1.txtpassword.focus();
      }else
         document.form1.submit();
最简单的一个javascript验证,alert的功能是实现了,怎么焦点就是回不来呢?请教了

解决方案 »

  1.   

    代码很奇怪,
    if(document.form1.txtusername.value=="")                 
          {
          alert("您的用户名忘记填写了!"); 
          document.form1.txtpassword.focus();     
          }    
    为什么txtusername.value为空串的时候要把焦点放在txtpassword上呢?
      

  2.   

      function onSubmit() {
                    if(document.form1.txtusername.value=="")                 
                    {
                        alert("您的用户名忘记填写了!"); 
                        document.form1.txtusername.focus();     
                    }     
                    else if(document.form1.txtpassword.value=="")     
                    {
                        alert("您的密码忘记填写了!");
                        document.form1.txtpassword.focus();
                    }else
                        document.form1.submit(); 
                }
    测试没问题。
    如果用的是ie浏览器的话,如果javascript有错误,浏览器左下角会出现黄色叹号,打开可以查看是什么错误。
    如果用firefox就更方便调试了。自己根据运行效果调试看看。
      

  3.   

    哦 对不起哈 把修改的那个的给粘上来了
    if(document.form1.txtusername.value=="")                  
          { 
          alert("您的用户名忘记填写了!");  
          document.form1.txtusrname.focus();      
          }      
          else if(document.all.txtpassword.value=="")      
          { 
            alert("您的密码忘记填写了!"); 
            document.form1.txtpassword.focus(); 
          }else 
            document.form1.submit(); 
      

  4.   

    晕了 我用的是myeclipse6.0 username框 和password框什么也没输,倒是弹出用户名为空的提示,点了确定后,焦点不再任何一个框中
      

  5.   

    判断的时候document.form1.txtusername.value==null是不是也要写上啊。
      

  6.   

    alert("您的用户名忘记填写了!");  
    document.form1.txtusrname.focus();  这里应该是txtusername 不是 txtusrname
      

  7.   

    应该要先判断==null 吧
    然后再判读==""的情况我昨天把这两个判别写反了报错调了好久才找到原因
      

  8.   

      function loginsubmit()
      {  
          if(document.form1.txtusername.value==null)                   
          {
            alert("您的用户名忘记填写了!");  
            document.form1.txtusername.focus();
          }       
          else if(document.all.txtpassword.value==null)      
          {
             alert("您的密码忘记填写了!");
             document.form1.txtpassword.focus();
          }else
             document.form1.submit();
      }现在的代码  启动tomcat后报错了~~~~ 昨天还没报错呢!
    本人的纠错能力不强,错误信息如下:
    java.net.BindException: Address already in use: JVM_Bind:8080
    at org.apache.tomcat.util.net.JIoEndpoint.init(JIoEndpoint.java:501)
    at org.apache.tomcat.util.net.JIoEndpoint.start(JIoEndpoint.java:515)
    at org.apache.coyote.http11.Http11Protocol.start(Http11Protocol.java:203)
    at org.apache.catalina.connector.Connector.start(Connector.java:1131)
    at org.apache.catalina.core.StandardService.start(StandardService.java:531)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
    2008-7-8 10:13:24 org.apache.catalina.startup.Catalina start
    严重: Catalina.start: 
    LifecycleException:  service.getName(): "Catalina";  Protocol handler start failed: java.net.BindException: Address already in use: JVM_Bind:8080
    at org.apache.catalina.connector.Connector.start(Connector.java:1138)
    at org.apache.catalina.core.StandardService.start(StandardService.java:531)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
      

  9.   

    if(document.form1.txtusername.value=="")                 
          {
          alert("您的用户名忘记填写了!"); 
          document.form1.txtusrname.focus();  
       return false;
          }     
          else if(document.all.txtpassword.value=="")     
          {
            alert("您的密码忘记填写了!");
            document.form1.txtpassword.focus();
    return false;
          }else
            document.form1.submit(); 
      

  10.   

    java.net.BindException: Address already in use: JVM_Bind:8080 
    端口好像已经被占用了.
    看一下,是不是有另外的tomcat启动了
      

  11.   

    确定没有开其他的tomcat 关了myeclipse重新打开加载 启动还是一样的错误信息~~~~
      

  12.   

    if(document.form1.txtusername.value=="")                  
          { 
          alert("您的用户名忘记填写了!");  
          document.form1.txtusername.focus();      
          }      
          else if(document.form1.txtpassword.value=="")      
          { 
            alert("您的密码忘记填写了!"); 
            document.form1.txtpassword.focus();       
          }else 
            document.form1.submit(); 表单名字统一的 from1, 你好好看看吧!LZ
      

  13.   

    你先不用eclipse里的tocmat,直接关闭一次tomcat然后再用eclipse里的,打开tomcat试试