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
广告