formmethod属性仅对<input type="submit">和<input type="image">生效,可原生覆盖<form>的method;不支持button、reset等类型,IE9及以下不兼容。

formmethod 属性确实能覆盖 <form> 的 method
只要 input 类型是 submit 或 image,且设置了 formmethod,浏览器就会用它替代 <form> 上的 method 值。这不是“模拟”或 JS 干预,是原生 HTML 行为,所有现代浏览器都支持。
哪些 input 类型支持 formmethod
只有两类触发提交的按钮才生效:
-
<input type="submit">—— 最常见,显式提交 -
<input type="image">—— 图片按钮,点击时也提交
以下类型**不支持**,设了 formmethod 会被忽略:<input type="button">、<input type="reset">、<input type="text"> 等。注意:即使加了 onclick 手动调用 form.submit(),也不会走 formmethod。
formmethod 和 formaction 一起用时的优先级
如果同时设置 formmethod 和 formaction,两者都以按钮为准,互不影响。但要注意:
立即学习“前端免费学习笔记(深入)”;
-
formaction覆盖的是<form>的action,不是method - 若只设
formmethod没设formaction,请求仍发往<form>的action地址 - 若表单本身没写
method(即默认GET),而按钮写了formmethod="POST",那就真用POST
示例:
<form action="/search"> <input name="q" value="test"> <input type="submit" value="GET 搜索" formmethod="GET"> <input type="submit" value="POST 提交" formmethod="POST" formaction="/api/submit"> </form>
第一个按钮发 GET /search?q=test,第二个发 POST /api/submit(带 body)。
容易被忽略的兼容性与调试陷阱
看似简单,但实际踩坑点不少:
- IE10+ 支持
formmethod,IE9 及更早版本完全忽略该属性,会退回到<form>的method—— 如果需要兼容 IE9,得用 JS 拦截submit事件手动改method - Chrome/Firefox/Safari 对空字符串
formmethod=""的处理不一致:有的当无效值回退到表单默认值,有的直接报错或忽略;建议只用明确值:"GET"或"POST" - 如果按钮在表单外(靠
form="form-id"关联),formmethod依然有效 —— 但必须确保form属性指向的表单存在且已解析完成,否则行为未定义
真正关键的不是“能不能用”,而是确认目标浏览器是否在你的支持范围内,以及有没有混用 JS 提交逻辑干扰了原生行为。



















