简体   繁体   中英

Update Image source from AJAX success function

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);
    }
});

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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