最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Update Image source from AJAX success function - Stack Overflow

programmeradmin0浏览0评论

I use to update Label values inside the AJAX success function like below, But I need to know how I'm going to apply this method to change/update "src" of an <img id="myimage" src=""/>

$.ajax({
    url: 'clmcontrol_livematchupdate',
    type: 'post',
    dataType: 'json',

    success: function (data) {

        $('#mstatus').html(data.matchstatus);
        // $('#myimage').... ?

    },
    complete: function () {
        // Schedule the next request when the current one has been completed
        setTimeout(ajaxInterval, 4000);
    }
});

I use to update Label values inside the AJAX success function like below, But I need to know how I'm going to apply this method to change/update "src" of an <img id="myimage" src=""/>

$.ajax({
    url: 'clmcontrol_livematchupdate',
    type: 'post',
    dataType: 'json',

    success: function (data) {

        $('#mstatus').html(data.matchstatus);
        // $('#myimage').... ?

    },
    complete: function () {
        // Schedule the next request when the current one has been completed
        setTimeout(ajaxInterval, 4000);
    }
});
Share Improve this question edited Nov 21, 2013 at 5:37 Tushar Gupta - curioustushar 57.1k24 gold badges105 silver badges109 bronze badges asked Nov 21, 2013 at 5:35 Dilukshan MahendraDilukshan Mahendra 3,3989 gold badges44 silver badges63 bronze badges 1
  • $("#elementId").attr("src","value"); – Aditya Singh Commented Nov 21, 2013 at 5:38
Add a comment  | 

3 Answers 3

Reset to default 10

Using jquery, You can use like $("#myimage").attr('src','img url');

Assume, you have response like data.imgsrc then it should be like, $("#myimage").attr(src, data.imgsrc);

$.ajax({
        url: 'clmcontrol_livematchupdate',
        type: 'post',
        dataType: 'json',

        success: function (data) {

            $('#mstatus').html(data.matchstatus);
            $("#myimage").attr('src','img url');

        },
        complete: function () {
            // Schedule the next request when the current one has been completed
            setTimeout(ajaxInterval, 4000);
        }
    });
$('#myimage').attr('src', '/imagePath/');

Try .prop()

success: function (data) {
    $('#mstatus').html(data.matchstatus);
    $('#myimage').prop('src', 'VAlue'); //change image src
}


Read .prop() vs .attr()

发布评论

评论列表(0)

  1. 暂无评论