简体   繁体   English

如何使用jquery ajax同时更新两个div?

[英]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. 但是问题是,当我同时更新我的​​喜欢时,它也必须更新不喜欢的div

this is my .js for dislike: 这是我不喜欢的.js:

$('.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的方式对其进行修改:

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

Here is my HTML : 这是我的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. 更新:我必须同时获取有关喜欢和不喜欢的数据,然后将该数据放入两个不同的div中。 There is a table with like and dislike column. 有一个带有“ like和“ dislike列的表。 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. 但是,如果某个人在喜欢某个特定帖子后不喜欢,那么不喜欢将更新为+1,喜欢将更新为-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. 如果我说对了,那么您想从数据库中获取数据并使用一个jquery调用将其显示在两个不同的div中。

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. 成功后,只需过滤数据并将其放在不同的div中即可。 Hope it helped. 希望能有所帮助。 :) :)

If I understand correctly you just want to update the number of likes and dislikes in your divs. 如果我理解正确,那么您只想更新div中喜欢和不喜欢的次数。

$('.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);
           }
       }
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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