简体   繁体   English

Ajax成功响应加载到div

[英]ajax success response load to divs

I have three forms and using this jquery function 我有三种形式,并使用此jQuery函数

$('form').submit(function() {
    $.ajax({
        type: $(this).attr('method'),
        url: $(this).attr('action'),
        data: $(this).serialize(),
        success: function(response) {
            $('#setInfo').fadeOut('500').empty().fadeIn('500').append(response);
        }
    });
        return false;
    });

to submit the form datas, but with this function i am stuck at loading the response at one particular div. 提交表单数据,但是使用此功能,我只能在一个特定的div上加载响应。

The data i send always have action=email , action=settings , etc depending on the form. 根据表单,我发送的数据始终具有action = emailaction = settings等。

So how i can use it to load the response of settings in another div and email in another div and all other default in current div. 因此,我如何使用它来加载另一个div中设置的响应,并在另一个div中发送电子邮件以及当前div中的所有其他默认值。

Thank You. 谢谢。

You could use a javascript variable to capture the intended target before submitting the request via AJAX then use the contents of that variable to determine the DIV to populate when the request returns. 在通过AJAX提交请求之前,可以使用javascript变量捕获预期的目标,然后使用该变量的内容确定在请求返回时要填充的DIV。

$('form').submit(function() {
    var target = $('#action').val();
    $.ajax({
        type: $(this).attr('method'),
        url: $(this).attr('action'),
        data: $(this).serialize(),
        success: function(response) {
            var div = $('#setInfo');
            if (target == 'settings') {
               div = $('#settings');
            }
            else if (target == 'email') {
               div = $('#email');
            }
            div.fadeOut('500').html(response).fadeIn('500');
        }
    });
        return false;
});

Note I also changed it to set the HTML with the response before fading it in to avoid the possibility of it "blinking." 请注意,我还对其进行了更改,以使其在淡入之前设置带有响应的HTML,以避免其“闪烁”。

added a id of target to then 然后添加了目标ID

var target = $(this).find('#target').val();

later used tvanfossons code for loading 后来用tvanfossons代码加载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM