nciaer 发表于 2022-1-7 11:43:11

html里点击button为什么会提交表单?

页面里经常需要用到按钮,用<button>的时候发现,点击它会自动提交表单,不知道为啥,因为我提交表单一般都是用<input type= "submit">的,百度了下,原来button的type属性默认是submit,也就是提交功能,如果想用它当作普通按钮,需要指定type="button",也就是:<button type = "button">clickme</button>

刚才用js测试了下,发现button的type属性默认确实是submit。
<form>
    <button id = "btn">hello</button>
</form>
<script>
    function foo() {
      var o = document.getElementById('btn');
      console.log(o.type);
      return false;
    }
</script>

页: [1]
查看完整版本: html里点击button为什么会提交表单?