serializeArray() trong jQuery Ajax

serializeArray() trong jQuery Ajax

serializeArray() trong jQuery Ajax

Learn more »


Định nghĩa serializeArray() trong jQuery Ajax

Phương thức serializeArray() trong jQuery sắp xếp theo thứ tự tất cả Form và phần tử Form như phương thức .serialize() nhưng trả về một cấu trúc dữ liệu JSON để bạn làm việc với nó.

Cấu trúc JSON được trả về không là một chuỗi. Bạn phải sử dụng một plugin hoặc thư viện thứ ba để "stringify" (chuỗi hóa).

Cú pháp serializeArray() trong jQuery Ajax

Sau đây là cú pháp cho serializeArray() trong jQuery:

$.serializeArray( )

Tham số

Dưới đây miêu tả chi tiết về các tham số được sử dụng trong phương thức serializeArray() trong jQuery:

  • NA

Ví dụ minh họa serializeArray() trong jQuery Ajax

Giả sử chúng ta có nội dung PHP sau trong serialize.php file:

<?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; } ?>

Sau đây là ví dụ đơn giản minh họa cách sử dụng của phương thức serializeArray() trong jQuery:

<html>    <head>       <title>The jQuery Example</title>       <script type="text/javascript" src="http://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(                    "../serialize.php",                   $("#testform").serializeArray(),                   function(data) {                      $('#stage1').html(data);                   }                );                var fields = $("#testform").serializeArray();                $("#stage2").empty();                jQuery.each(fields, function(i, field){                   $("#stage2").append(field.value + " ");                });             });          });       </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>

Xem thêm tổng hợp jQuery Ajax


Lượt xem : 315

Integrations
Users

Share Profile

Anyone at KeenThemes can view
Anyone with link can edit

Give Award

Anyone at KeenThemes can view
Anyone with link can edit

Report User

Let us know why you’re reporing this person
Don't worry, your report is completely anonymous; the person you're
reporting will not be informed that you've submitted it