1

我正在尝试使用从带有 ajax 的 php 处理程序获取的图像路径更改图像源。

我正在捕捉的是:“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;
        }
    })
}

当我单击要更改的图像时,ajax 就完成了它的工作。也许我以不正确的方式使用响应数据。我使用控制台日志查看我得到了什么,它返回了一个对象。所以我想我的问题是:我现在正在做的事情不起作用,我不知道为什么。如果你想让我澄清任何事情,请告诉我。提前致谢。

4

4 回答 4

2

利用

$('.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;
            }
     });
}
于 2013-05-17T04:56:38.733 回答
0

在您的示例中$(this).attr("src", data);$(this) 指向 ajax 请求。要更改图像,您可以提供图像 ID,如下所示$('#imageId').attr("src", data);

于 2013-05-17T05:05:21.190 回答
0

像这样试试

var my_data = data;
var f = my_data.find('img').attr("src");

或者你可以直接使用

$(data).find('img').attr('src');
于 2013-05-17T04:55:33.667 回答
0

对于数据位置图像,请确保在数据库中,例如 http:http://localhost/new_accemo_upd_1/data/scan-yr-201 ...

从添加数据:

data.append('gambar', $('input[type=file]')[0].files[0]);

保存数据(服务器)

'gambar' : base_url($_FILES['gambar'])

如果我们要编辑图像:

$.ajax({
    url : "<?php echo site_url(name-url)?>/",
    type: "GET",
    dataType: "JSON",
    success: function(data)
    {
       $('[name="logo"]').attr("src", data.logo);

    },
    error: function (jqXHR, textStatus, errorThrown)
    {
        alert('Error get data from ajax');
    }
});

查看 HTML:

<img src="" name="logo"/>
于 2017-07-17T10:30:23.257 回答