<button></button> 与<input type="button">最开始我没有去吧二者区分,就是感觉都是button而已,但是最近做一个表单的提交时,注意到了问题
<button></button>竟然像<input type="submit">那样提交了表单,让我找了很久,当时我是不想让他提交的
下面的示例代码,是我在菜鸟上面在线写的,拷贝即可查看效果,至于更多的解释暂时未找到,日后更新附上详细参考
<meta charset="utf-8">
<script>
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == null || x == "") {
alert("需要输入名字。");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="demo_form.php"
οnsubmit="return validateForm()" method="post">
名字: <input type="text" name="fname">
<input type="submit" value="提交">//可以提交
<button>提交2</button>//可以提交
<input type="button" value="提交3">//不能提交
</form>
</body>
</html>
