我想做,验证输入框中如果为空不能提交,但是为什么我加了return false 但是还能提交到那个页面呢
<script type="text/javascript">
document.write("wode");
document.close();
function jiancha(){
var username=document.getElementById("username").value;
var userpassword=document.getElementById("userpassword").value;
if(username==""){
alert ("用户名不可为空");
return false;
}
if(userpassword==""){
alert ("密码不可为空");
return false;
}
}</script>
<body>
<form id="form1" name="form1" method="post" action="hhh.html">
<p>
<label for="textfield"></label>
用户名
<input type="text" name="username" id="usrname" />
</p>
<p>密码
<label for="textfield2"></label>
<input type="text" name="userpassword" id="userpassword" />
</p>
<p>
<input type="submit" name="button" id="button" value="提交" onclick="jiancha();" />
<input type="reset" name="button2" id="button2" value="重置" />
</p>
</form>
<script type="text/javascript">
document.write("wode");
document.close();
function jiancha(){
var username=document.getElementById("username").value;
var userpassword=document.getElementById("userpassword").value;
if(username==""){
alert ("用户名不可为空");
return false;
}
if(userpassword==""){
alert ("密码不可为空");
return false;
}
}</script>
<body>
<form id="form1" name="form1" method="post" action="hhh.html">
<p>
<label for="textfield"></label>
用户名
<input type="text" name="username" id="usrname" />
</p>
<p>密码
<label for="textfield2"></label>
<input type="text" name="userpassword" id="userpassword" />
</p>
<p>
<input type="submit" name="button" id="button" value="提交" onclick="jiancha();" />
<input type="reset" name="button2" id="button2" value="重置" />
</p>
</form>
<script type="text/javascript">
function jiancha(){
var username = document.getElementById("username").value;
var userpassword = document.getElementById("userpassword").value;
if(username==""){
alert ("用户名不可为空");
return false;
}
if(userpassword==""){
alert ("密码不可为空");
return false;
}
return true;
}</script>
<body>
<form id="form1" name="form1" method="post" action="hhh.html" onsubmit="return jiancha();">
<p>
<label for="textfield"></label>
用户名
<input type="text" name="username" id="usrname" />
</p>
<p>密码
<label for="textfield2"></label>
<input type="text" name="userpassword" id="userpassword" />
</p>
<p>
<input type="submit" name="button" id="button" value="提交" />
<input type="reset" name="button2" id="button2" value="重置" />
</p>
</form>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function jiancha(){
var username = document.getElementById("usrname").value;
var userpassword = document.getElementById("userpassword").value; if(username==""){
alert ("用户名不可为空");
return false;
}
if(userpassword==""){
alert ("密码不可为空");
return false;
}
return true;
}</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="a.jsp" >
<p>
<label for="textfield"></label>
用户名
<input type="text" name="username" id="usrname" value=""/>
</p>
<p>密码
<label for="textfield2"></label>
<input type="text" name="userpassword" id="userpassword" value=""/>
</p>
<p>
<input type="submit" name="button" id="button" value="提交" onclick="return jiancha()"/>
<input type="reset" name="button2" id="button2" value="重置" />
</p>
</form>
</body>
</html>
如果问题还在,找你的程序关键点, <input type="submit" name="button" id="button" value="提交" onclick="jiancha();" />
这是你的
<input type="submit" name="button" id="button" value="提交" onclick="return jiancha()"/>
这是关键。
加油哦!