开发者

update span content with ajax

开发者 https://www.devze.com 2023-03-12 22:13 出处:网络
How could I make the votes update directly? Just like the votes on this sit开发者_如何学Goe I have this function:

How could I make the votes update directly? Just like the votes on this sit开发者_如何学Goe

I have this function:

function ratePost(id, rating) {
    $.post("ratepost.php", {postID: id, rating: rating}, function(data){alert(data+" return val"); });
}

<span class="karma"><?php echo $rating ?></span>


i think you need to do 2 things in order for it to work:

  1. set a id to your display span and leave it empty (no need for the php echo), eg

<span id = 'rating'...></span>

  1. in your ajax callback function, do something like

function(data){document.getElementById("rating").innerHTML = data;}


Seems you are with a javascript library like jQuery.

So you can target your span like that in your callback :

function(data){ $(".karma").html(data) }

Here it is what you want (I think :-)) :

function ratePost(id, rating) {
    $.post("ratepost.php", {postID: id, rating: rating}, function(data){
        var current = $(".karma").html();
        $(".karma").html( current + 1 );
    });
}

<span class="karma"><?php echo $rating ?></span>
0

精彩评论

暂无评论...
验证码 换一张
取 消