当前位置: 首页 > 文档资料 > jQuery 入门教程 >

serialize()

优质
小牛编辑
132浏览
2023-12-01

描述 (Description)

serialize( )方法将一组输入元素序列化为一串数据。

语法 (Syntax)

以下是使用此方法的简单语法 -

<i>$</i>.serialize( )

参数 (Parameters)

以下是此方法使用的所有参数的说明 -

  • NA

例子 (Example)

假设我们在serialize.php文件中有以下PHP内容 -

<?php
if( $_REQUEST["name"] ) {
   $name = $_REQUEST['name'];
   echo "Welcome ". $name;
   $age = $_REQUEST['age'];
   echo "<br />Your age : ". $age;
   $sex = $_REQUEST['sex'];
   echo "<br />Your gender : ". $sex;
}
?>

以下是一个简单的例子,简单地显示了这种方法的用法 -

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("#driver").click(function(event){
               $.post( 
                  "/jquery/serialize.php",
                  $("#testform").serialize(),
                  function(data) {
                     $('#stage1').html(data);
                  }
               );
               var str = $("#testform").serialize();
               $("#stage2").text(str);
            });
         });
      </script>
   </head>
   <body>
      <p>Click on the button to load result.html file:</p>
      <div id = "stage1" style = "background-color:blue;">
         STAGE - 1
      </div>
      <br />
      <div id = "stage2" style = "background-color:blue;">
         STAGE - 2
      </div>
      <form id = "testform">
         <table>
            <tr>
               <td><p>Name:</p></td>
               <td><input type = "text" name = "name" size = "40" /></td>
            </tr>
            <tr>
               <td><p>Age:</p></td>
               <td><input type = "text" name = "age" size = "40" /></td>
            </tr>
            <tr>
               <td><p>Sex:</p></td>
               <td> <select name = "sex">
                  <option value = "Male" selected>Male</option>
                  <option value = "Female" selected>Female</option>
               </select></td>
            </tr>
            <tr>
               <td colspan = "2">
                  <input type = "button" id = "driver" value = "Load Data" />
               </td>
            </tr>
         </table>
      </form>
   </body>
</html>