|
1 |
| -button_formmethod.md |
| 1 | +HTML \<button> formmethod 属性 |
2 | 2 | ===
|
3 | 3 |
|
4 |
| -欢迎您编辑 <a target="__blank" href="https://github.com/jaywcjlove/html-tutorial/blob/main/docs/tags/button_formmethod.md">docs/tags/button_formmethod.md</a> 文件,共建 HTML Tutorial 文档。 |
| 4 | +## 示例 |
| 5 | + |
| 6 | +带有两个提交按钮的表单。 第一个提交按钮用 `method="get"` 提交表单数据,第二个用 `method="post"` 提交表单数据: |
| 7 | + |
| 8 | +```html idoc:preview:iframe |
| 9 | +<form action="/action_page.php" method="get"> |
| 10 | + <label for="fname">First name:</label> |
| 11 | + <input type="text" id="fname" name="fname"><br><br> |
| 12 | + <label for="lname">Last name:</label> |
| 13 | + <input type="text" id="lname" name="lname"><br><br> |
| 14 | + <button type="submit">Submit</button> |
| 15 | + <button type="submit" formmethod="post">Submit using POST</button> |
| 16 | +</form> |
| 17 | +``` |
| 18 | + |
| 19 | +## 定义和用法 |
| 20 | + |
| 21 | +`formmethod` 属性指定发送表单数据时使用的 HTTP 方法。此属性覆盖表单的“方法”属性。 |
| 22 | + |
| 23 | +`formmethod` 属性仅用于具有 `type="submit"` 的按钮。 |
| 24 | + |
| 25 | +表单数据可以作为 URL 变量(使用 `method="get"`)或作为 HTTP post(使用 `method="post"`)发送。 |
| 26 | + |
| 27 | +`get` 方法的注意事项: |
| 28 | + |
| 29 | +* 它以名称/值对的形式将表单数据附加到 URL |
| 30 | +* 这对于用户想要为结果添加书签的表单提交很有用 |
| 31 | +* 您可以在 `URL` 中放置多少数据是有限制的(因浏览器而异),因此,您无法确定所有表单数据都会正确传输 |
| 32 | +* 切勿使用 `get` 方法传递敏感信息! (密码或其他敏感信息将显示在浏览器的地址栏中) |
| 33 | + |
| 34 | +关于 `post` 方法的注意事项: |
| 35 | + |
| 36 | +* 它将表单数据作为 HTTP 发布事务发送 |
| 37 | +* 使用 `post` 方式提交的表单无法添加书签 |
| 38 | +* 它比 `get` 更健壮和安全 |
| 39 | +* 它没有尺寸限制 |
| 40 | + |
| 41 | +## 浏览器支持 |
| 42 | + |
| 43 | +表中的数字指定了完全支持该属性的第一个浏览器版本。 |
| 44 | + |
| 45 | +| 属性 Attribute | ![chrome][1] | ![edge][2] | ![firefox][3] | ![safari][4] | ![opera][5] | |
| 46 | +| ---- | ---- | ---- | ---- | ---- | ---- | |
| 47 | +| formmethod | 9.0 | 10.0 | 4.0 | 5.1 | 15.0 | |
| 48 | +<!--rehype:style=width: 100%; display: inline-table;--> |
| 49 | + |
| 50 | +## 语法 |
| 51 | + |
| 52 | +```html |
| 53 | +<button type="submit" formmethod="get|post"> |
| 54 | +``` |
| 55 | + |
| 56 | +## 属性值 |
| 57 | + |
| 58 | +| 值 Value | 描述 Description | |
| 59 | +| ----- | ----- | |
| 60 | +| get | 将表单数据附加到 URL: `URL?name=value\&name=value` | |
| 61 | +| post | 将表单数据作为 HTTP 发布事务发送 | |
| 62 | +<!--rehype:style=width: 100%; display: inline-table;--> |
| 63 | + |
| 64 | +[1]: ../assets/chrome.svg |
| 65 | +[2]: ../assets/edge.svg |
| 66 | +[3]: ../assets/firefox.svg |
| 67 | +[4]: ../assets/safari.svg |
| 68 | +[5]: ../assets/opera.svg |
0 commit comments