javaScrip表单提交问题
<script type="text/javascript">
function CheckFrom() {
var username = document.getElementById("username").value;
var userpwd = document.getElementById("userpwd").value;
var reuserpwd = document.getElementById("reuserpwd").value;
var realname = document.getElementById("realname").value;
var str = /^\d+$/.test(username);
if (username == "") {
document.getElementById("msg").innerHTML = "用户名不能为空";
} else if (!str) {
document.getElementById("msg").innerHTML = "只能输入数字";
return false;
} else if (userpwd == "") {
document.getElementById("msgpwd").innerHTML = "密码不能为空";
return true;
} else if (userpwd != reuserpwd) {
document.getElementById("msgrepwd").innerHTML = "密码请输入一致";
return false;
} else if (realname == "") {
document.getElementById("msgrealname").innerHTML = "请输入真实姓名";
return false;
} else {
document.getElementById("msg").innerHTML = "";
document.getElementById("msgpwd").innerHTML = "";
document.getElementById("msgrepwd").innerHTML = "";
document.getElementById("msgrealname").innerHTML = "";
return true;
}
}
</script>
<form action="../AddUser" method="post" name="From" onsubmit="CheckFrom()">
为什么还是会提交
------解决方案-------------------- You can override this event by returning false in the event handler
你可以通过返回false 来阻止form提交,但是你没判断失败的时候没有全部返回false啊?
------解决方案-------------------- onsubmit=" return CheckFrom()" .....
------解决方案-------------------- 探讨 <form action="../AddUser" method="post" name="From" onsubmit="CheckFrom()">
------解决方案-------------------- onsubmit="return CheckFrom()"
------解决方案-------------------- 探讨 onsubmit=" return CheckFrom()" .....
------解决方案-------------------- onsubmit=" return CheckFrom()"?
然后你的if else语句里面,除了最后一个return true;
其他的都加上return false