function check(){
var name = document.getElementById("name").value;
if(name == null || name == ''){
alert("用户名不能为空");
return false;
}
return true;
}
<form name="form" action="跳转的页面" method="post" onsubmit="return check()">
<input type="text" id="name"/>
<input type="sumit" value="提交"/>
</form>
需要注意的是,οnsubmit=“return check()”中的return是一定要加上的,不然check的返回值哪怕是false,仍然提交。也就是说,οnsubmit=“return false”为不执行提交;οnsubmit=“return true”或οnsubmit=“return ”都执行提交。