Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 投票:AJAX更新计数_Javascript_Php_Jquery_Ajax_Laravel 4 - Fatal编程技术网

Javascript 投票:AJAX更新计数

Javascript 投票:AJAX更新计数,javascript,php,jquery,ajax,laravel-4,Javascript,Php,Jquery,Ajax,Laravel 4,我正在实现一个投票系统,当我想使用AJAX更新投票数时,我会刷新所有出版物的计数。但我不能只刷新一份出版物。请参阅我的HTML: @foreach($publications as $publication) <li class="like-publication"> <span>{{HTML::image(URL::to('img/widgets/likesocialmeet.png'),'Like')}}</span> <span class

我正在实现一个投票系统,当我想使用AJAX更新投票数时,我会刷新所有出版物的计数。但我不能只刷新一份出版物。请参阅我的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

您正在使用选择所有

$('.votes').text( data );
您需要将其限制为窗体的兄弟

var votes = $(this).prev('.votes');
votes.text( data );
所以你的javascript看起来像-

$(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;
   });
});
JSFIDLE示例-

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