<%@page contentType="text/html" pageEncoding="SHIFT_JIS" %>
<html>
<head>
<title>login</title>
</head>
<script language="javascript">
<%!
        function sub()
        {
         String arg = document.getElementById("username").value;
    String arg1 = document.getElementById("password").value;
String message = "";
if ((arg.equals("1")) && (arg1.equals("1"))) {
    message = "success";
} else {
     message = "failed";
       }
application.setAttribute("Message",message);
document.Form.action = "message.jsp";
    }
%>
</script>
<body>
<body bgcolor="white">
<h2>login</h2><p>
<form name="Form" method="post" action="" onSubmit="">
<table border=1 >
<tr>
<td width="80" align="center">username:
</td>
<td><input width="100%" type="text" id="username" name="username" />
</td>
</tr>
<tr>
<td width="80" align="center">password:
</td>
<td><input width="100%" type="password" id="password" name="password" maxLength="8" />
</td>
</tr>
</table><p>
<input type="submit" value="submit" onClick="sub();" />
</form>
</body>
sub()中为什么无法获取text id 为username,password的值?

解决方案 »

  1.   

    var arg   =   document.getElementById("username").value; 
    var arg1   =   document.getElementById("password").value; 
      

  2.   

    <%@page   contentType="text/html"   pageEncoding="SHIFT_JIS"   %> 
    <html> 
    <head> 
    <title> login </title> 
    </head> 
    <script   language="javascript"> 
    <%! 
                    function   sub() 
                  { 
                  String   arg   =   document.getElementById("username").value; 
            String   arg1   =   document.getElementById("password").value; 
    String   message   =   ""; 
    if   ((arg.equals("1"))   &&   (arg1.equals("1")))   { 
            message   =   "success"; 
    }   else   { 
            message   =   "failed"; 
                  } 
    application.setAttribute("Message",message); 
    document.Form.action   =   "message.jsp"; 
            } 
    %> 
    </script> 
    <body> 
    <body   bgcolor="white"> 
    <h2> login </h2> <p> 
    <form   name="Form"   method="post"   action=""   onSubmit=""> 
    <table   border=1   > 
    <tr> 
    <td   width="80"   align="center"> username: 
    </td> 
    <td> <input   width="100%"   type="text"   id="username"   name="username"   /> 
    </td> 
    </tr> 
    <tr> 
    <td   width="80"   align="center"> password: 
    </td> 
    <td> <input   width="100%"   type="password"   id="password"   name="password"   maxLength="8"   /> 
    </td> 
    </tr> 
    </table> <p> 
    <input   type="submit"   value="submit"   onClick="sub();"   /> 
    </form> 
    </body> 
    sub()中为什么无法获取text   id   为username,password的值?把上面红颜色的去掉试一试
      

  3.   

    在javascript怎么能有这个"<%!..%>",去掉它就好
      

  4.   

    楼主混淆了js和jsp的语句,都写在一起了。我改了一下,楼主看看:<%@page   contentType="text/html"   pageEncoding="SHIFT_JIS"   %> 
    <html> 
    <head> 
    <title> login </title> 
    </head> 
    <script   language="javascript">  function sub()
        { 
            String   arg   = document.getElementById("username").value; 
            String   arg1   = document.getElementById("password").value; 
    String   message   =   ""; 

    if((arg=="1") and (arg1=="1"))

             message   =   "success"; 
    }else

             message   =   "failed"; 
            } 
    document.getElementById("Message").value=message; 
    document.Form.action   =   "message.jsp"; 
        } 
     
    </script> 
    <body> 
    <body   bgcolor="white"> 
    <h2> login </h2> <p> 
    <form   name="Form"   method="post"   action=""   onSubmit=""> 
    <table   border=1   > 
    <tr> 
    <td   width="80"   align="center"> username: 
    </td> 
    <td> <input   width="100%"   type="text"   id="username"   name="username"   /> 
    </td> 
    </tr> 
    <tr> 
    <td   width="80"   align="center"> password: 
    </td> 
    <td> <input   width="100%"   type="password"   id="password"   name="password"   maxLength="8"   /> 
    </td> 
    </tr> 
    </table> <p> 
    <input type="hidden" name="Message" value="">
    <input   type="submit"   value="submit"   onClick="sub();"   /> 
    </form> 
    </body> 
    </html>
    我把你的这句:application.setAttribute("Message",message); 用隐藏域来代替,同样可以实现你的功能。
      

  5.   

    纠正一下:上面的:if((arg=="1") and (arg1=="1"))
    应该改为:if((arg=="1") && (arg1=="1"))
      

  6.   

    谢谢大家的帮助!
    <%!
    %>
    删掉以后页面问题点击按钮仍然无法跳转至message.jsp页面,是不是点击按钮根本就没有调用sub()方法的原因,如果是如何解决啊