写了个简单的登录页面,只要判断用户名和密码栏里是否为空,如果为空弹出对话框,确定后返回为空的栏。但现在有2个问题:1.对话框弹出后不返回为空的那栏。
    2.用户名栏输入了,密码栏为空,弹出对话框后确定,页面被刷新了,用户名里面输入的值也跟着没了。
    我试了很多方法但都没法解决。

解决方案 »

  1.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    <script type="text/javascript" language="javascript">
    function fun()
    {
    var aa=document.getElementById("texname");
    var bb=document.getElementById("texpassword");
    if(aa.value=="")
    {
    alert("用户名为空");
    aa.focus();
    return false;
    }
    else if(bb.value=="")
    {
    alert("密码框为空");
    bb.focus(); 
    return false;
    }
    else
    {
    return true;
    }
    }
    </script>
    <body>
    <form id="form1" name="form1" method="post" action="" onsubmit="return fun()">
      <p>
        用户名:
          <input type="text" name="textfield" id="texname" />
      </p>
      <p>
        密码:
          <input type="text" name="textfield2" id="texpassword" />
        <input type="submit" name="button" id="button" value="提交"/>
      </p>
    </form>
    </body>
    </html>本机测试可用,说明!
    问题1: 提交事件写函数fun()不是写在按钮上这里 onsubmit="return fun()"
    问题2: 提交后返回焦点bb.focus() 用到了focus()这个方法。
    如果都不为空就会提交表单. 返回真.
      

  2.   

    写了个简单的登录页面,只要判断用户名和密码栏里是否为空,如果为空弹出对话框,确定后返回为空的栏。但现在有2个问题:1.对话框弹出后不返回为空的那栏。 
    那就就让哪个TextBox为空就让它得到焦点。document.getElementById("Text1").focus()    2.用户名栏输入了,密码栏为空,弹出对话框后确定,页面被刷新了,用户名里面输入的值也跟着没了。 
        我试了很多方法但都没法解决。
    客户端alert("密码不能为空");就不会刷新页面。
      

  3.   

    先谢谢楼上2位 但问题依旧 下面是我的代码 麻烦各位来挑挑错页面
    <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
    + request.getServerName() + ":" + request.getServerPort()
    + path + "/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page"> <link href="styles/Log.css" rel="stylesheet" type="text/css">
    <script language="javascript" type="text/javascript" src="js/login.js"></script>
    </head> <body>
    <form name="login" method="get">
    <div id="main">
    <h1 id="title">
    用户登陆
    </h1>
    <h2 id="login">
    请输入用户名密码
    </h2>
    <p class="formt">
    用户名
    </p>
    <p>
    <input type="text" name="username" class="username" id="textname" />
    </p>
    <p class="formt">
    密码
    </p>
    <p>
    <input type="password" name="password" class="password"
    id="textpassword" />
    </p>
    <p id="forget">
    <a href="reg.jsp">注册新用户</a>&nbsp;
    <a href="ddd">忘记密码</a>
    </p>
    <p id="button">
    <input type="image" src="images/login_login.gif" class="imgbutton" onclick="pd()" />
    </p>
    </div>
    </form>
    </body>
    </html>
    js
    function pd() {
    var tn = document.getElementById("textname");
    var tp = document.getElementById("textpassword");
    if(tn.value==""){
    alert("用户名不能为空!!!");
    tn.focus();

    }
    if(tp.value==""){
    alert("密码不能为空!!!");
    tp.focus();

    }

    }
      

  4.   


    <input type="image" src="images/login_login.gif" class="imgbutton" onclick="pd()" /> 
    这样写会导致 form提交改为
    <input type="image" src="images/login_login.gif" class="imgbutton" onclick="if(pd()){ return true;}else{ return false;}" /> function pd() {
    var tn = document.getElementById("textname");
    var tp = document.getElementById("textpassword");
    if(tn.value==""){
    alert("用户名不能为空!!!");
    tn.focus();
    return false;
    }
    if(tp.value==""){
    alert("密码不能为空!!!");
    tp.focus();
    return false;
    }
    return true;
    }
      

  5.   

    加一个 hiddenframe 
    <IFRAME id=hiddenframe src="submit.jsp" width=0 height=0></IFRAME>
    submit.jsp:<form name="form1" method="post" action="pass.jsp">
      <input type="hidden" name="textname">
      <input type="hidden" name="textpassword">  
    </form> 
    function pd() : function pd() { 
    var tn = document.getElementById("textname"); 
    var tp = document.getElementById("textpassword"); 
    if(tn.value==""){ 
    alert("用户名不能为空!!!"); 
    tn.focus(); } 
    else if(tp.value==""){ 
    alert("密码不能为空!!!"); 
    tp.focus(); } 
    else{
    document.frames["hiddenframe"].location.replace("submit.jsp");
    hiddenframe.form1.textname.value=tn;
    hiddenframe.form1.textpassword.value=tp;
    hiddenframe.form1.submit();    }
    }