serializeArray不适用于从ajax响应生成的字段

时间:2015-05-13 11:01:34

标签: jquery ajax

$(document).on("click","#addressModal",function(){
            var addressId=$(this).attr("caddressId");

            $.ajax({
                url: "ajax/customerDetails_ajax.php",
                type: "POST",
                data: "get=getAddressDetails&addressId="+addressId,

                success: function(result){
                    $("#getAddress").html(result)
                },
                error: function(result){
                    alert("Error")
                }
            })

        })

$(document).on("click","#updateCustomerAddressDetails",function(){

            console.log($("#serviceLocation").val())

            var data = $("#addressEditForm").serializeArray()
            console.log(data)
            $.ajax({
                url: "ajax/customerDetails_ajax.php",
                type: "POST",
                data: data,
                dataType: "json",
                success: function(result){
                    //console.log(result)
                    if(result.status == "success"){
                        window.location.reload();
                    }
                },
                error: function(result){
                    alert("Error")
                }
            })
        });
<div class="modal fade" id="editAddress">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title">Edit Address</h4>
                        </div>
                        <div class="modal-body">
                            <form id="addressEditForm" method="post">
                                <div id="getAddress">
                                    <!-- address details will be displayed here via ajax response -->
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            <button type="button" class="btn btn-primary" id="updateCustomerAddressDetails" name="updateCustomerAddressDetails">Save changes</button>
                        </div>
                    </div><!-- /.modal-content -->
                </div><!-- /.modal-dialog -->
            </div><!-- /.modal -->

我正在尝试序列化从ajax响应中获取的数据并将其发送到另一个ajax响应。但是,由于数据是从ajax获取的,因此数据没有被序列化。它会生成一个空白数组。

1 个答案:

答案 0 :(得分:0)

如果我没有在ajax响应中包含该表单,则它不会缓存序列化的值。

现在,我在ajax响应中的表单中发送封装输出。