Prototype - Form request() 方法

此方法是一种方便的方法,用于通过 Ajax.Request 将表单序列化并提交到表单的 action 属性的 URL。options 参数传递给 Ajax.Request 实例,允许覆盖 HTTP 方法并指定其他参数。

传递给 request() 的选项会与底层 Ajax.Request 选项智能合并 −

  • 如果表单具有 method 属性,则其值将用于 Ajax.Request 方法选项。如果将 method 选项传递给 request(),则它优先于表单的 method 属性。如果未指定任何内容,则方法默认为"POST"。

  • 参数选项中指定的键值对(作为哈希或查询字符串)将与序列化的表单参数合并(并优先于序列化的表单参数)。

语法

formElement.request([options]);

返回值

它返回一个新的 Ajax.Request。

示例 1

考虑以下示例e −

<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>单击按钮查看结果。</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>单击按钮查看结果。</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

下面是另一个示例,向您展示如何覆盖 HTTP 方法并添加一些参数,只需在选项中使用方法和参数即可。在此示例中,我们将方法设置为 GET,并设置两个固定参数:兴趣和爱好。后者已存在于表单中,但此值将优先。

<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>单击按钮查看结果。</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.html