weixin_33711641 2014-05-11 21:47 采纳率: 0%
浏览 69

投票:AJAX更新计数

I'm implementing a voting system and I when I want to update the number of votes using AJAX, I refresh the count of all the publications. But I cant to refresh only one publication. See my HTML:

@foreach($publications as $publication)
<li class="like-publication">

 <span>{{HTML::image(URL::to('img/widgets/likesocialmeet.png'),'Like')}}</span>

 <span class="votes">
   {{Publication::find($publication->id)->profilepublicationvotes->sum('vote')}}
 </span>

 {{ Form::open(array('url' => 'profilepublicationvotelike', 'class' => 'vote_ajax')) }}

 <input type="text" id="disabledTextInput" style="display:none" name="vote" value='1'>
 <input type="text" id="disabledTextInput" style="display:none" name="user_id" value="{{ Auth::user()->id }}">
 <input type="text" id="disabledTextInput" style="display:none" name="publication_id" value="{{ $publication->id }}">

 {{ Form::submit('vote', array('class' => 'button expand round')) }}

 {{ Form::close() }}

</li>
@endforeach

And my javascript (AJAX):

$(document).ready(function() {
   var form = $('.vote_ajax');
    form.bind('submit',function () {
        $.ajax({
            type: $(this).attr('method'),
            url: $(this).attr('action'),
            data: $(this).serialize(),
            complete: function(data){
            },
            success: function (data) {
                $('.success_message').hide().html('');
                    $(form)[0].reset();
                    console.log("working");

                    $('.votes').text( data );
            },
            error: function(errors) {
                    alert('Something went to wrong.Please Try again later...');
            }               
        });
   return false;
   });
});
  • 写回答

1条回答 默认 最新

  • weixin_33705053 2014-05-12 02:49
    关注

    You are selecting all <span class="votes"> by using

    $('.votes').text( data );
    

    You need to limit it to just the sibling of the form

    var votes = $(this).prev('.votes');
    votes.text( data );
    

    So your javascript would look like -

    $(document).ready(function() {
       var form = $('.vote_ajax');
        form.bind('submit',function () {
            //get the sibling vote
            var votes = $(this).prev('.votes');
            $.ajax({
                type: $(this).attr('method'),
                url: $(this).attr('action'),
                data: $(this).serialize(),
                complete: function(data){
                },
                success: function (data) {
                    $('.success_message').hide().html('');
                        $(form)[0].reset();
                        console.log("working");
    
                        // update just the sibling vote
                        votes.text( data );
                },
                error: function(errors) {
                        alert('Something went to wrong.Please Try again later...');
                }               
            });
       return false;
       });
    });
    

    jsFiddle Example - http://jsfiddle.net/GhMfM/1/

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建