一个小问题

目的是:运行一下程序后,如果用户名、密码均不为空,则进行后续处理,否则显示相应信息并定位到空的地方。
实际:不输入任何内容,点“提交”后继续执行ASP文件。

怎么回事,请诸位指教:
源代码:

 1<html>
 2<head>
 3<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 4<title>无标题文档</title>
 5<script>   
 6<!--   
 7function check()   
 8{   
 9if(form.username.value=="")   
10{   
11alert("用户名不能为空");   
12form.username.focus();   
13return false;   
14}   
15if(form.userpwd.value=="")   
16{   
17alert("密码不能为空");   
18form.userpwd.focus();   
19return false;   
20}   
21form.submit();   
22}   
23//-->   
24</script>
25</head>
26<body>
27<div align="center">
28<h1>成绩查询系统</h1>
29<form action="asp/submit.asp" method="post" name="form">
30<table border="0" cellspacing="0" width="100%">
31<tr>
32<td width="45%"><div align="right">用户名</div></td>
33<td width="55%"><input name="username" type="text"/></td>
34</tr>
35<tr>
36<td>
37<div align="right">密码</div></td>
38<td><input name="userpwd" type="password"/></td>
39</tr>
40<tr>
41<td><div align="right">
42<input name="submit" onsubmit="javascript:check()" type="submit" value="提交"/>
43</div></td>
44<td><input name="reset" type="reset" value="清除"/></td>
45</tr>
46</table>
47</form>
48<p> </p>
49</div>
50</body>
51</html>

---------------------------------------------------------------

1<input name="submit" onsubmit="javascript:check()" type="submit" value="提交"/>

改成

1<input name="submit" onsubmit="javascript: return check()" type="submit" value="提交"/>

---------------------------------------------------------------
或者

1<form action="asp/submit.asp" method="POST" name="form" onsubmit="return check()">
2<input name="username" type="text"/>
3<input name="userpwd" type="password"/>
4<input name="submit" type="submit" value="提交"/>
5</form>
 1<script language="JavaScript">   
 2function check(){   
 3{   
 4if (document.form.username.value == ""){   
 5alert("请填写用户名");   
 6return false;   
 7}   
 8if (document.form.userpwd.value == ""){   
 9alert("请填写密码");   
10return false;   
11}   
12return true;   
13}   
14}   
15</script>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus