Button formMethod 屬性
定義和用法
formMethod
屬性設置或返回按鈕的 formmethod 屬性 的值。
formmethod 屬性規定發送表單數據時使用的 HTTP 方法。此屬性覆蓋表單的 method 屬性。
formmethod 屬性僅用于 type="submit" 的按鈕。
表單數據可以作為 URL 變量(使用 method="get")或作為 HTTP post(使用 method="post")發送。
關于 "get" 方法的注意事項:
- 它將表單數據以名稱/值對的形式附加到 URL
- 這對于用戶想要對結果添加書簽的表單提交很有用
- 可在 URL 中放置的數據量是有限制的(因瀏覽器而異),因此,您不能確定所有表單數據都會正確傳輸
- 切勿使用 "get" 方法傳遞敏感信息!(密碼或其他敏感信息將顯示在瀏覽器的地址欄中)
關于 "post" 方法的注意事項:
- 它將表單數據作為 HTTP post 事務發送
- 使用 "post" 方法提交的表單無法添加被書簽
- 比 "get" 更健壯和安全
- 沒有尺寸限制
注釋:formmethod 屬性 是 HTML5 中 <button> 元素的新屬性。
實例
例子 1
返回發送表單數據時使用的 HTTP 方法:
var x = document.getElementById("myBtn").formMethod;
例子 2
更改發送表單數據的方法:
document.getElementById("myBtn").formMethod = "post";
例子 3
另一個返回 formMethod 屬性的例子:
var x = document.getElementById("myBtn").formMethod;
語法
返回 formMethod 屬性:
buttonObject.formMethod
設置 formMethod 屬性:
buttonObject.formMethod = get|post
屬性值
值 | 描述 |
---|---|
get | 將表單數據附加到 URL: URL?name=value&name=value |
post | 將表單數據作為 HTTP post 事務發送 |
技術細節
返回值: | 字符串值,表示用于向服務器提交表單的 HTTP 方法。 |
---|
瀏覽器支持
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
支持 | 10.0 | 支持 | 支持 | 支持 |
相關頁面
HTML 參考手冊:HTML <button> formmethod 屬性