获得从Ajax响应图像的src [英] Get image src from ajax response

查看:547
本文介绍了获得从Ajax响应图像的src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变图像源的图像路上我从一个PHP处理程序使用Ajax获得。

I'm trying to change an image source with an image path I'm getting from a php handler with ajax.

什么我赶上是:IMG / blah.jpg

What I'm catching is: "img/blah.jpg"

这是我的ajax:

$('.name').click( function() {
    //var e = $(this).attr('src','img/no.png'); //this works

$.ajax({ 
        url:'handler.php?blahid='.$idnum,
        type:"GET", 
        success: function(data)
        {
            var f = $(this).attr("src", data); //$(this) is the image I click on that I want to replace
            console.log(f);
        },
        error: function(jqXHR, textStatus, errorThrow)
        {
            debugger;
        }
    })
}

阿贾克斯,它的工作,当我点击我想改变形象。也许我用了不正确的方式响应数据。我用的控制台日志,看看我得到了什么,它返回一个对象。所以我想我的问题是:我在做什么,现在不工作,我不知道为什么。让我知道如果你要我澄清什么。先谢谢了。

The ajax does its job when I click on the image I want to change. Perhaps I'm using the response data in an incorrect way. I used console log to see what I got and it returned an object. So I guess my problem is: what I'm doing now is not working and I'm not sure why. Let me know if you want me to clarify anything. Thanks in advance.

推荐答案

使用

$('.publish').click( function() {
    var self=this; // add this line   
    $.ajax({ 
            url:'handler.php?blahid='.$idnum,
            type:"GET", 
            success: function(data)
            {
                var f = $(self).attr("src", data); // use self instead of this
                console.log(f);
            },
            error: function(jqXHR, textStatus, errorThrow)
            {
                debugger;
            }
     });
}

这篇关于获得从Ajax响应图像的src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆