0

在我的表单中,我在电子邮件输入按钮失去焦点后进行 ajax 调用。我正在使用返回的响应数据来填充模态。然后在模态中,用户可以单击一个按钮来选择他们想要的记录并使用该数据填充表单。我正在传递按钮上的数据 ID。

一切正常,直到我单击模式中的按钮。我猜那时ajax响应不再可用?

所以我的问题是:我是否必须再次进行 ajax 调用才能再次查找数据,或者它仍然是我可以引用的地方?

这是我的代码:

$(document).ready(function () {
    $(document).on('click', '.choosebtn', function () {
        console.log(this);
        $(".myModal").modal("hide");
        $('.myform').populate({
            response.DATA[event.target.id]
        }, {
            phpNaming: true,
            debug: true
        });
    });

    $('.email').blur(email_check);
});

function email_check() {
    var email = $('.email').val();
    var data = $('form').serialize();

    if (email == "" || email.length < 10) {
        $('.email').css('border', '3px ##CCC solid');
    } else {
        jQuery.ajax({
            type: "POST",
            url: "/managemembers/checkemail?format=json",
            data: data,
            dataType: "json",
            cache: false,
            success: function (response) {
                if (response.DATA) {
                    var trHTML = '';
                    $.each(response.DATA, function (index, value) {
                        trHTML += '<tr><td width="25%">' 
                        + value[0] + '</td><td width="25%">' 
                        + value[1] + '</td><td width="25%">' 
                        + value[2] + '</td><td width="25%">' 
                        + value[3] + '</td><td><button type="button" class="choosebtn" id=' 
                        + index + '>Choose</button></td></tr>';
                    });
                    $('.mytable').append(trHTML);

                    $(".myModal").modal("show");
                }
            }
        });
    }
}
4

3 回答 3

0

response是 AJAXsuccess函数的本地函数,因此您将无法访问您的populate函数。一个快速的解决方法是将 分配response给一个全局变量:

$(document).ready(function(){
    var ajaxResponseData;
    ..
    //Reference global in populate
    $('.myform').populate({ajaxResponseData.DATA......
    ..
    ..
    ..
    success: function (response) {
        ajaxResponseData = response;
});
于 2013-10-11T14:53:27.247 回答
0

目前尚不清楚您在哪里尝试访问您无法访问的响应,但是不,您将无法在回调之外访问它。但是,如果您以后需要它,您可以将响应保存在 ajax 调用之外的变量中。像这样:

var yourResponseVariable;

jQuery.ajax({
   type: "POST",
   url: "/managemembers/checkemail?format=json",
   data: data,
   dataType: "json",
   cache: false,
   success: function (response) {
            if(response.DATA){
            var trHTML = '';

            $.each(response.DATA, function (index, value) {
                trHTML += '<tr><td width="25%">' + value[0] + '</td><td width="25%">' + value[1] + '</td><td width="25%">' + value[2] + '</td><td width="25%">' + value[3] + '</td><td><button type="button" class="choosebtn" id=' + index + '>Choose</button></td></tr>';
            });

            $('.mytable').append(trHTML);

            yourResponseVariable = response;

            $(".myModal").modal("show");
        }    
   }

});

根据您的需要,您还可以保存其中的一部分而不是整个响应。

于 2013-10-11T14:55:13.140 回答
0

一种选择是将您关心的值存储在隐藏字段中并使用 jQuery 遍历函数来查找它:

所以目前在你的标记中,你最终得到了这个片段(以及其他东西):

<td>
    <button type="button" class="choosebtn" id='1'>Choose</button>
</td>

你可以改变它,这样你就得到了这个,隐藏字段包含你想要的信息:

<td>
    <button type="button" class="choosebtn" id='1'>Choose</button>
    <input type="hidden" class="myValue" />
</td>

然后在js中:

$(document).on('click', '.chooseBtn', function() {
    var myVal = $(this).siblings('.myValue').val();
});
于 2013-10-11T14:58:30.770 回答