例如:onClick='return checkForm()'
与 onClick='checkForm()'
的区别
JavaScript在事件中调用函数时用return返回值实际上是对window.event.returnValue进行设置。该值放在提交表单中的onclick事件中则不会提交表单,如果放到超链接中则不执行超链接,也就禁止或取消了请求。l
当在 Open 中
如果函数 checkForm() 返回 true, 那么 页面就会打开 test.html
否则, (返回 false), 那么页面不会跳转到 test.html, 只会执行你的 checkForm() 函数里的内容. (add函数中控制页面转到test.html除外)
而 Open
不管 checkForm() 返回什么值, 都会在执行完 add后打开页面test.html
下面是本人写的一个小Demo,如果发现有什么问题,多谢指正!
<form action="http://www.baidu.com" name="myform" onsubmit="return checkForm()">
username:
<input type="text" name="username">
password:
<input type="password" name="password">
<input type="submit" value="提交">
</form>
<script>
document.myform.username.focus();
function checkForm(){
if(document.myform.username.value == ""){
alert("请输入用户名!");
document.myform.username.focus();
return false;
}
if(document.myform.password.value == ""){
alert("请输入密码!");
document.myform.password.focus();
return false;
}
return true;
}
</script>