jQuery ajax调用后data-src没有被src取代

时间:2018-10-09 10:40:13

标签: jquery ajax

我正在将div与来自如下ajax函数的响应绑定

BindProductFamilyList: function (str) {
    var _obj= new Object();

    _obj.id= str;


    $.ajax({
        url: 'page.aspx/Bind',
        data: JSON.stringify({ 'obj': _obj}),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        cache: false,
        async: false,
        type: 'POST',
        beforeSend: function () {
            $("#divLoader").fadeIn();
        },
        complete: function () {
        },
        success: function (response) {
            var _strInnerHtml = "";
            $("#div").html(_strInnerHtml);
            // Loop and bind the parameter header
            $.each(response, function (key, value) {
                if (Object.keys(value).length > 0) {
                    $.each(value, function (key, value) {
                        // Loop and bind 
                        _strInnerHtml += "<div class='col-sm-6 col-md-4 col-lg-3 mb-5'>";
                        _strInnerHtml += "<a href='#' title='View Details'class='prod-box wow fadeInUp'>"; 
                        _strInnerHtml += "<img class='img-fluid'  src='data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='  data-src='../uploads/pic/" + value.pic + "' title='" + value.name + "' alt='" + value.name + "' />";
                        _strInnerHtml += "<h2>" + value.name + "</h2>";
                        _strInnerHtml += "</a>";
                        _strInnerHtml += "</div>";
                    });
                }
                else {

                    _strInnerHtml = "No records.";
                }
            });
            $("#div").html(_strInnerHtml);
            $("#divLoader").fadeOut();

        },
        error: function (xhr, ajaxOptions, thrownError) {
            //alert(xhr.status);
            //alert(thrownError);
        }
    });
}



  $(document).on("change", ".catfilter", function () {
    ns.BindProductFamilyList(ns.GetSelectedId());
});

首次调用此函数时,图像已正确加载。当我在复选框列表的更改事件上调用此函数时。不会加载图像,只会加载数据64图像,而不会加载文件夹中的实际图像。我该如何解决?

1 个答案:

答案 0 :(得分:0)

尝试添加img.onLoad事件,并将img.src设置为该事件的结果。...

img.onload(function(){img.src = "imgurl.png"})