formmethod 是 button 或 input[type="submit"] 的原生属性,能覆盖所在 form 的 method 属性,仅对该按钮的单次提交生效,需满足在 form 内、非 type="button"、浏览器支持三条件才有效。

formmethod 是什么,它真能绕过表单默认 method 吗
formmethod 是 <button></button> 或 <input type="submit"> 的原生属性,作用是**覆盖其所在 <form></form> 元素的 method 属性**,仅对该按钮的提交行为生效。它不是“重写表单默认方式”的通用开关,而是“单次提交覆盖”,不影响其他按钮或直接 submit() 调用。
常见误解是以为设了 formmethod 就能永久改变整个表单行为 —— 实际上,它只在点击该按钮时起效;表单本身 method 仍保持原值,其他按钮、JS 的 form.submit()、回车提交等都照旧走原 method。
怎么给 button 写 formmethod 才真正生效
必须同时满足三个条件,formmethod 才会实际参与请求发送:
-
button或input[type="submit"]必须在<form></form>内部(或通过form="form-id"显式关联) - 该元素不能有
type="button"(否则不触发提交) - 浏览器必须支持该属性(Chrome 10+、Firefox 4+、Safari 5.1+、Edge 全支持;IE10+ 支持,IE9 及以下完全忽略)
示例:
点击这个按钮,实际发出的是 POST /api/user 请求,携带 q=test,而非 GET。
formmethod 和 formaction 配合使用的典型场景
单独改 method 很少够用,多数时候需要连带改提交地址,比如同一个表单里区分「保存草稿」和「正式提交」:
-
formmethod="post"+formaction="/draft"→ 草稿存接口 -
formmethod="put"+formaction="/user/123"→ 更新用户资源 -
formmethod="delete"+formaction="/user/123"→ 删除操作(注意:部分老浏览器对delete支持弱,实际常靠后端兼容POST+_method=DELETE)
注意:formmethod 值必须是合法 HTTP 方法名(get、post、put、delete、patch),大小写不敏感,但建议全小写;非法值(如 FORMMETHOD="Get" 或拼错)会被降级为 GET。
容易被忽略的坑:fetch / axios 提交时 formmethod 完全无效
formmethod 是纯 HTML 表单提交机制的一部分,只影响原生表单提交(submit 事件 + 浏览器跳转或页面刷新)。一旦你用 JS 阻止默认行为,比如:
form.addEventListener('submit', e => {
e.preventDefault();
fetch(form.action, { method: form.method }); // 这里读的是 form.method,不是按钮的 formmethod
});
那么 formmethod 就彻底失效 —— 因为浏览器根本没走原生提交流程。此时想复现相同逻辑,得手动解析点击的按钮:
form.addEventListener('submit', e => {
const submitter = e.submitter;
const method = submitter?.formMethod || form.method;
const action = submitter?.formAction || form.action;
// 然后 fetch(action, { method })
});
这个 e.submitter 是关键,但 IE 不支持,需 fallback 判断 event.target 类型。
前端入门到VUE实战笔记:立即使用
在学习笔记中,你将探索 前端 的入门与实战技巧!











