在form表单中写onsubmit="return onCheck();";
再在js中通过重写onCheck()方法进行判断:
返回false则不提交表单,返回true时才进行表单的提交
<body>
<form action="register.php" method="post" accept-charset="utf-8" onsubmit='return onCheck();'>
<input type="text" name="userName" value="" placeholder="" id="name">
<input type="password" name="pass" value="" placeholder="" id="pass">
<input type="submit" name="" value="提交" id="subm">
<input type="reset" name="" value="重置">
</form>
<script type='text/javaScript'>
function onCheck(){
var name=document.getElementById('name');
var pass=document.getElementById('pass');
if(name.value.trim()==''||pass.value.trim()==''){
alert('用户名或密码不能为空!');
return false;
}else{
return true;
}
}
</script>
</body>