
- 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.Request將表單序列化並提交到表單操作屬性的URL。options引數傳遞給Ajax.Request例項,允許覆蓋HTTP方法並指定附加引數。
傳遞給request()的選項將與底層的Ajax.Request選項智慧地合併:
如果表單具有method屬性,則其值將用於Ajax.Request method選項。如果將method選項傳遞給request(),則它優先於表單的method屬性。如果兩者都沒有指定,則method預設為“POST”。
在parameters選項中指定的鍵值對(作為雜湊或查詢字串)將與序列化表單引數合併(並優先於它們)。
語法
formElement.request([options]);
返回值
它返回一個新的Ajax.Request。
示例1
考慮以下示例:
<html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function postIt() { var form = $('example'); form.request(); //done - it's posted } </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 = "Post It" onclick = "postIt();"/> </body> </html>
輸出
示例2
可能還有另一個示例,您可以在回撥函式中執行某些操作:
<html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function postIt() { var form = $('example'); form.request({ onComplete: function() { alert('Form data saved!') } }) } </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 = "Post It" onclick = "postIt();"/> </body> </html>
輸出
示例3
這是一個更進一步的示例,展示瞭如何透過在選項中簡單地使用method和parameters來覆蓋HTTP方法並新增一些引數。在這個示例中,我們將方法設定為GET,並設定兩個固定引數:interests和hobbies。後者已存在於表單中,但此值將優先。
<html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function postIt() { var form = $('example'); form.request({ method: 'get', parameters: { interests:'JavaScript', 'hobbies[]':['programming', 'music'] }, onComplete: function() { alert('Form data saved!') } }) } </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 = "Post It" onclick = "postIt();"/> </body> </html>
輸出
prototype_form_management.htm
廣告