用ajax更新span内容

How could I make the votes update directly? Just like the votes on this site

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>