form表单提交验证不过,为什么会提交,求解

<body>
<div id="bg_img"></div>
<div id="login_div" >
<img id="user_div" src="touxiang.png"/>
<div id="form_div">
<form id="realform_div" action="/StuInforManagement/login" method="post" onsubmit="return check();">
<div >
<input name="user" id="input_div" type="text" placeholder="用户名" />
</div>
<div>
<input name="password" id="password_div" type="password" placeholder="密码" />
</div>
<div>
<input id="rememberbox_div" type="checkbox" onchange="SaveUser();" />
<span id="span_div1">记住密码</span>

<span id="span_div2">自动登录</span>
<input id="auto_div" type="checkbox" onchange="Auto();" />
</div>
<input id="submit_div" type="submit" value="登 录">
<span id="forgetSpan">忘记密码?,快<a href="">找回</a></span>
</form>
</div>
</div>
<script type="text/javascript">
function check(){
var name = document.getElementById("input_div");
var password = document.getElementById("password");
if(name.value==""){
alert("用户名不能为空");
return false;

}else if(password.value==""){

alert("密码不能为空")
return false;
}else{
return true;
}

}
</script>
</body>

比如 页面
<form action ="..." methot ="post">
<input type ="text" name ="username" id="username">
<input type ="password" name ="password" id ="password">
<input type ="submit" value ="提交" onclick ="return valid();">
</form>

JavaScript代码;;
<script type="text/javascript">
function valid(){
var name = document.getElementById("username").value;
var pass = document.getElementById("password").value;
if(name==""){
alert("用户名不能为空");
return false;
}
if(pass==""){
alert("密码不能为空");
return false;
}
return true;
}
</script>

注意:document.getElementById("username").value;和
document.getElementById("password").value;
这里引号 里面的名字 一定要和 页面 控件的名字相同。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-04-10
[3] 词条图册 更多图册