简体   繁体   中英

how to simultaneously update two div using jquery ajax?

I am designing a like and dislike feature. But problem arise when i update my like as the same time it must update the dislike div too.

this is my .js for dislike:

$('.dislike').click(function(e) {
    dislike = $(this);
    to_dislike= dislike.data('val');
    $.ajax({
        type     : 'POST',
        url      : '<?php echo site_url('/url.php');?>',
        data     : 'dislikes='+to_dislike,
        dataType : 'text',
        success  : function(data){
            $("#dislike_"+to_dislike).html(data);
        },
        error    : function(){
            $("#dislike_"+to_dislike).html('Sorry');
        }
    });
});

Its working good. but i want to modify it in such a way that it should simultaneously update my div:

<div id='like'></div>

Here is my HTML :

<table>
    <tr>
        <td> <button class="like"></button></td>
        <td> <button class="dislike"></button></td>
    </tr>
    <tr>
        <td>LIKE : <div id='like'></div></td>
        <td> Dislike : <div id='dislike'></div></td>
    </tr>
</table>

EDIT:

Update: I have to fetch data about like and dislike at same time and then put that data in two different div's. There is a table with like and dislike column. On like i update my likes. But if a person dislike after liking a particular post then dislike is updated to +1 and like is updated to -1.

please help..

If i am getting you right then u want to get data from database and display it in two different div using one jquery call.

Why don't you concatenate the string with some symbol like $ and then when echo it. On Success just filter the data and put it in different div. Hope it helped. :)

If I understand correctly you just want to update the number of likes and dislikes in your divs.

$('.dislike').click(function(e) {
   dislike = $(this);
   to_dislike= dislike.data('val');
   $.ajax({
       type     : 'POST',
       url      : '<?php echo site_url('/url.php');?>',
       data     : 'dislikes='+to_dislike,
       dataType : 'text',
       success  : function(data){
           var dislike = parseInt($("#dislike").html());
           var like = parseInt($("#like").html());
           if ( dislike >= 0 && like >= 0) {
              $("#dislike").html(dislike + 1);
              $("#like").html(like - 1);
           }
       }
   });
});

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