Prototype-form-serializeelements

提供:Dev Guides
移動先:案内検索

プロトタイプ-serializeElements()メソッドのフォーム

このメソッドは、フォーム要素の配列をAjaxリクエストに適した文字列(デフォルトの動作)にシリアル化します。オプションのgetHashがtrueと評価される場合、キーがフォームコントロール名で値がデータであるオブジェクトハッシュです。

フォームをシリアル化するための推奨される方法は、_Form.serialize_です。 ただし、_serializeElements_を使用すると、選択した特定の入力要素をシリアル化できます。

構文

Form.serializeElements(elements [,getHash = false]);

戻り値

Stringオブジェクトを返します。

次の例を考慮してください-

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>

      <script>
         function showResult() {
            var form = $('example');
            var arr = form.getInputs('text');
            var element = Form.serializeElements( arr );
            alert("Serialized String : " + 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>

出力