submit和button提交表单的区别

时间:2021-07-05 20:10:35
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>submit</title>
<script type="text/javascript">
function checkForm(){
if(document.form1.userName.value.length==0){
alert("请输入用户名!");
return false;
}
return true;
document.form1.submit();
}
</script>
</head>
<body>
<form name="form1" method="post" action="ygdacx.html" onsubmit="return checkForm()">
<input type="text" name="userName" size="10" />
<input type="submit" value="提 交" />
</form>
</body>
</html>

当类型为submit的input提交form表单时,配合onsubmit写事件。

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<title>button</title>
<script type="text/javascript">
function checkForm(){
if(document.form1.userName.value.length==0){
alert("请输入用户名!");
return false;
}
document.form1.action="/email.php"
document.form1.submit();
}
</script>
</head>
<body>
<form name="form1" method="post">
<button type="button" value="提 交" onclick="checkForm()"></button>
</form>
</body>

使用button的话是先走一个onclick事件,在事件末端指向要跳转的php页面即可。