
- Prototype 教程
- Prototype - 首頁
- Prototype - 簡要概述
- Prototype - 有用的特性
- Prototype - 實用方法
- Prototype - 元素物件
- Prototype - 數字處理
- Prototype - 字串處理
- Prototype - 陣列處理
- Prototype - 雜湊處理
- Prototype - 基本物件
- Prototype - 模板
- Prototype - 列舉
- Prototype - 事件處理
- Prototype - 表單管理
- Prototype - JSON 支援
- Prototype - AJAX 支援
- Prototype - 表示範圍
- Prototype - 週期性執行
- Prototype 實用資源
- Prototype - 快速指南
- Prototype - 實用資源
- Prototype - 討論
Prototype - 表單序列化方法
此方法用於將表單資料序列化為適合 Ajax 請求(預設行為)的字串,或者如果可選引數 getHash 評估為 true,則序列化為一個物件雜湊,其中鍵是表單控制元件名稱,值是資料。
根據可選引數 getHash 是否評估為 true,結果將是 {name: "johnny", color: "blue"} 形式的物件或 "name = johnny&color = blue" 形式的字串,適合 Ajax 請求中的引數。
語法
formElement.serialize([getHash = false]);
返回值
它返回一個 String 物件。
下面是兩個關於它的工作原理的提示。請詳細檢視下面的示例。
$('example').serialize() // 'username = sulien&age = 22&hobbies = coding&hobbies = hiking' $('example').serialize(true) // {username: 'sulien', age: '22', hobbies: ['coding', 'hiking']}
示例
<html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { var form = $('example'); var element = form.serialize(); alert("form.serialize() : " + element.inspect()); } </script> </head> <body> <p>Click the button to see the result.</p> <br /> <form id = "example" action = "#" onsubmit = "return false"> <fieldset> <legend>User info</legend> <div> <label for = "username">Username:</label> <input name = "username" id = "username" value = "Sulien" type = "text"> </div> <div> <label for = "age">Age:</label> <input name = "age" id = "age" value = "23" size = "3" type = "text"> </div> <div> <label for = "hobbies">Your hobbies are:</label> <select name = "hobbies" id = "hobbies" multiple = "multiple"> <option>coding</option> <option>swimming</option> <option>hiking</option> <option>drawing</option> </select> </div> </fieldset> </form> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html>
輸出
prototype_form_management.htm
廣告