简体   繁体   English

在ajax内部运行函数成功回调

[英]Run a function inside ajax success call back

i have a snippet i created which checks all of the images inside a div and adds a class dependant on their size. 我创建了一个代码段,该代码段可检查div中的所有图像并根据其大小添加一个类。

I have this currently on document ready and i works perfectly fine when the page is loaded. 我目前已经在文档上准备好了,加载页面时我可以很好地工作。

However, im making a CMS where you can edit text on the page itself and then it updates via ajax. 但是,我正在制作一个CMS,您可以在其中编辑页面本身上的文本,然后通过ajax更新文本。

The call response normally looks like: 呼叫响应通常如下所示:

success: function (response) {
    if (response.databaseSuccess) {
        $("#container #" +response.postid).load("#container #" +response.postContentID);
        $targetForm.find(".saveupdatebutton").qtip("hide");
    }
}

After this, the images inside the div that is loaded via the .load() function are not resized. 此后,不会调整通过.load()函数加载的div中的图像的大小。

I have tried putting the code i use in that success response but no luck. 我尝试将我使用的代码放入成功响应中,但没有运气。

How should i be calling the code after the response? 响应后我应该如何调用代码?

Heres the code: 这是代码:

$(document).ready(function () {
// check each image in the .blogtest divs for their width. If its less than X make it full size, if not its poor and keep it normal
var box = $(".blogtest");
box.find("img.buildimage").on('load', function () {
    var img = $(this),
        width = img.width();
    if (width >= 650) {
        img.addClass("buildimage-large");
    } else if (width < 500 && width > 101) {
        img.addClass("buildimage-small");
    }
    // if image is less than X, its most likely a smiley
    else if (width < 100) {
        img.addClass("buildimage-smiley");
    }
    }).filter(function () {
        //if the image is already loaded manually trigger the event
        return this.complete;
    }).trigger('load');
});

Depending on which version of jQuery you are using, you might be able to just change .on to .live like this: 根据所使用的jQuery版本,您可以像这样将.on更改为.live

box.find("img.buildimage").live('load', function () {

If that does NOT work, then you can try below. 如果那不起作用,那么您可以尝试以下操作。 Then you'd have to do it the "right" way below: 然后,您必须按以下“正确”方式进行操作:

First externalize your function for the image resizing: 首先将您的函数外部化以调整图像大小:

$(document).ready(function () {
    // check each image in the .blogtest divs for their width. If its less than X make it full size, if not its poor and keep it normal
    var box = $(".blogtest");
    box.find("img.buildimage").on('load', imageOnload)
        .filter(function () {
            //if the image is already loaded manually trigger the event
            return this.complete;
        })
        .trigger('load');
});

function imageOnload(evt){
    var img = $(evt.currentTarget),
        width = img.width();
    if (width >= 650) {
        img.addClass("buildimage-large");
    } else if (width < 500 && width > 101) {
        img.addClass("buildimage-small");
    }
    // if image is less than X, its most likely a smiley
    else if (width < 100) {
        img.addClass("buildimage-smiley");
    }
}

Then, you can just add this statment in your success callback: 然后,您可以在成功回调中添加以下语句:

$("#container #" +response.postid).on("load", imageOnload);

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

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