[英]Javascript check form onsubmit
I've used event.preventDefault()
to stop the form
action and call the login()
, but when the login
function return true
, it still can't do the form action? 我已经使用
event.preventDefault()
停止了form
操作并调用了login()
,但是当login
函数返回true
,它仍然无法执行表单操作吗?
HTML: HTML:
<form onsubmit="event.preventDefault();return login();" action="index.php" method="get" id="register-form" >
Javascript: Javascript:
<script type="text/javascript">
function login() {
var password = window.prompt("Please input password", "");
if (password !== '123') {
alert('Password is incorrect');
return false;
} else {
alert('Success');
return true;
}
}
</script>
Remove event.preventDefault();
删除
event.preventDefault();
from the onsubmit
. 从
onsubmit
。 This will stop the form submission regardless of the value returned from login()
. 无论
login()
返回的值如何,都将停止提交表单。
function login() { var password = window.prompt("Please input password", ""); if (password !== '123') { alert('Password is incorrect'); return false; } else { alert('Success'); return true; } }
<form onsubmit="return login();" action="index.php" method="get" id="register-form"> <input type="submit" value="Login" /> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.