简体   繁体   English

Ajax 绑定点击到另一个元素

[英]Ajax bind click to another element

I have a button (is anchor tag with class vote) which should be clicked to vote for an article via ajax request.我有一个按钮(是带有类投票的锚标签),应该点击它通过 ajax 请求为一篇文章投票。 After that I want to update the votes count in my blade view (its a laravel application).之后,我想在我的刀片视图(它是一个 Laravel 应用程序)中更新投票计数。 The div with the class points should be updated but I dont know how to bind points on click to the ajax call?带有类点的 div 应该更新,但我不知道如何将点击点绑定到 ajax 调用?

I want to bind the .point to ajax call like I did it with $button to then update the div with the class points.我想将 .point 绑定到 ajax 调用,就像我用 $button 做的那样,然后用类点更新 div。

<a data-id="{{ $article->id }}" class="btn vote {{ Auth::check() && Auth::user()->votedFor($article) ? 'btn-success' : '' }}"></a>

Main code:主要代码:

<div data-id="{{ $article->id }}" class="points">{{ $article->votes->count() }}</div>

$(document).on('click','.vote',function(){
 var $counter = $(this).next();
 var $button = $(this);
 var id = $button.data('id');
 var token = $('meta[name="csrf-token"]').attr('content');
$.ajax({

  type:'POST',
  url:"{!! URL::to('article/vote/') !!}/" + id,
  dataType: 'JSON',
  data: {
      "_method": 'POST',
      "_token": token,
      "id": id,
  },
  success:function(data){
      $counter.html(data.count);
      var color = data.voted ? '#bbb' : '#38c172';
      $button.css('background-color', color);
      console.log('success');
      console.log(data);
  },
  error:function(xhr){
      if (xhr.status == 401) {
        window.location.href = "{!! URL::to('login') !!}";
      } else if (xhr.status == 403) {
        window.location.href = "{!! URL::to('email/verify') !!}";
      }
  },
});
});

So you want your .points to be updated and not the a button you clicked.所以你希望你的.points被更新,而不是你点击的a按钮。

First, make sure your a and .points are children of the same div element and there like:首先,确保您的a.points是同一个div元素的子元素,例如:

<div>
    <a data-id="{{ $article->id }}" class="btn vote {{ Auth::check() && Auth::user()->votedFor($article) ? 'btn-success' : '' }}">
        button
    </a>
    <div data-id="{{ $article->id }}" class="points">
        {{ $article->votes->count() }}
    </div>
</div>
...
<div>
    <a data-id="{{ $article->id }}" class="btn vote {{ Auth::check() && Auth::user()->votedFor($article) ? 'btn-success' : '' }}">
        button
    </a>
    <div data-id="{{ $article->id }}" class="points">
        {{ $article->votes->count() }}
    </div>
</div>

Then in your JavaScript define the counter element like然后在您的 JavaScript 中定义计数器元素,例如

var $counter = $(this).parent().find('points');

and update your success: method to apply CSS class and put data count:并更新您的success:应用 CSS 类和放置数据计数的方法:

  $counter.html(data.count);
  var color = data.voted ? '#bbb' : '#38c172';
  $counter.css('background-color', color);

Let me know if it makes sense and you can finish the implementation.让我知道它是否有意义,您可以完成实施。

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

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