首先我做了一个和数据库连接的登陆系统,刚做完,在完善项目,于是首先用js在登陆页面判断用户名和密码是否为空,如果为空则不提交到服务器,直接输出错误,肯定是js没写对的问题,因为没写js时能用。
贴代码<script type="text/javascript" >
function check(){
var oForm = document.forms("f1");
var txtName = oForm.name.value;
var txtPw = oForm.pw.value;
if(txtPw == "" or txtName == ""){
alert("用户名或密码不能为空");
return false;
}else(txtPw == null or txtName == null){
alert("用户名或密码不能为空");
return false;
}
}
</script> </head> <body>
<form method="post" action="servlet/LoginServlet name="f1">
<table align="center">
<tr><td>请登陆</td></tr>
<tr><td>用户名:<input type="text" name="name"></td></tr>
<tr><td>密 码:<input type="text" name="pw"></td></tr>
<tr><td>身 份:<input type="radio" name="type" value="学生" checked>学生
<input type="radio" value="教师" name="type">教师
<input type="radio" value="管理员" name="type">管理员</td></tr>
<tr><td> <input type="submit" value="提交" name="submit" onClick="check()">
<input type="reset" value="重置" name="reset"></td></tr>
</table> </form>
</body>
</html>前面还有一小部分无意义没贴不明白,起动tomcat,打开网页报的是404错误,没找到。
HTTP Status 404 - /CAM/servlet/LoginServlet%20name=type Status reportmessage /CAM/servlet/LoginServlet%20name=description The requested resource (/CAM/servlet/LoginServlet%20name=) is not available.
Apache Tomcat/6.0.13
贴代码<script type="text/javascript" >
function check(){
var oForm = document.forms("f1");
var txtName = oForm.name.value;
var txtPw = oForm.pw.value;
if(txtPw == "" or txtName == ""){
alert("用户名或密码不能为空");
return false;
}else(txtPw == null or txtName == null){
alert("用户名或密码不能为空");
return false;
}
}
</script> </head> <body>
<form method="post" action="servlet/LoginServlet name="f1">
<table align="center">
<tr><td>请登陆</td></tr>
<tr><td>用户名:<input type="text" name="name"></td></tr>
<tr><td>密 码:<input type="text" name="pw"></td></tr>
<tr><td>身 份:<input type="radio" name="type" value="学生" checked>学生
<input type="radio" value="教师" name="type">教师
<input type="radio" value="管理员" name="type">管理员</td></tr>
<tr><td> <input type="submit" value="提交" name="submit" onClick="check()">
<input type="reset" value="重置" name="reset"></td></tr>
</table> </form>
</body>
</html>前面还有一小部分无意义没贴不明白,起动tomcat,打开网页报的是404错误,没找到。
HTTP Status 404 - /CAM/servlet/LoginServlet%20name=type Status reportmessage /CAM/servlet/LoginServlet%20name=description The requested resource (/CAM/servlet/LoginServlet%20name=) is not available.
Apache Tomcat/6.0.13
<script type="text/javascript" >
function check(){
var oForm = document.forms("f1");
var txtName = oForm.name.value;
var txtPw = oForm.pw.value;
if(txtPw == "" || txtName == ""){
alert("用户名或密码不能为空");
return false;
}else(txtPw == null or txtName == null){
alert("用户名或密码不能为空");
return false;
}
}
</script>
</head>
<body>
<form method="post" action="servlet/LoginServlet" name="f1" onsubmit="return check()">
<table align="center">
<tr><td>请登陆</td></tr>
<tr><td>用户名:<input type="text" name="name"></td></tr>
<tr><td>密 码:<input type="text" name="pw"></td></tr>
<tr><td>身 份:<input type="radio" name="type" value="学生" checked>学生
<input type="radio" value="教师" name="type">教师
<input type="radio" value="管理员" name="type">管理员</td></tr>
<tr><td> <input type="submit" value="提交" onClick="check()">
<input type="reset" value="重置" ></td></tr>上面少个冒号,现在没有错误了,但是js不运行,毫无意义,就是说不打密码和用户名点提交,还是直接跳转到服务器,js没有进行判断
alert("用户名或密码不能为空");
return false;
}else{
//提交
}
只要试试。
var oForm = document.forms("f1");好像 var oForm = document.forms["f1"];
为了兼容浏览器最好使用ID来读取。
他说
SCRIPT5007: 属性“check”的值为 null、未定义或不是 Function 对象
login.jsp, 行54 字符90
就是
<tr><td> <input type="submit" value="提交" onClick="check()">
这行
保留form里的onsubmit就行function check(){
var oForm = document.forms("f1");
var txtName = oForm.name.value;
var txtPw = oForm.pw.value;
if(txtPw == "" || txtName == ""){
alert("用户名或密码不能为空");
return false;
}else(txtPw == null or txtName == null){
alert("用户名或密码不能为空");
return false;
}
return true; <--------------加上
}很多种方法可以得到input内的值,你可以先测试随便一个按钮,看能不到得到值,比如alert出来,然后在进行操作,或者用浏览器调试js