单击+和 - 按钮时增加和减少1个值

时间:2016-07-29 05:58:49

标签: jquery

我在jQuery中有一个总和计算过程,如下所示:

<th>Name</th>
<th>Vote</th>
<th>Action</th>

<tr>
   <td>User A</td>
   <td class="vote">5</td>
   <td>
     <a href="#" class="btn btn-lg btn-primary addVote">+</a>
     <a href="#" class="btn btn-lg btn-primary subVote">-</a>
   </td>
</tr>
<tr>
   <td>User B</td>
   <td class="vote">3</td>
   <td>
     <a href="#" class="btn btn-lg btn-primary addVote">+</a>
     <a href="#" class="btn btn-lg btn-primary subVote">-</a>
   </td>
</tr>

这是我的jQuery:

$(function() {
   $(".addVote").each(function() {
     var vote    = $('td.vote').text(),
         newVote = parseInt(suara) + parseInt(1);

     $(this).on('click', function() {
        $('td.vote').text(newVote);
     });
   });
});

我想要实现的是当我点击“+”按钮时, newVote 值应该取代投票值,并且每次“+”按钮都会继续此过程点击。如果单击“ - ”按钮,则应执行相同的过程。我只能这样做一次。下一步没有任何反应。我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:2)

我希望它适合你

    $(document).ready(function(){
         $('table').on('click','.addVote, .subVote', function() {
        var _vote= $(this).closest('tr').find('td.vote');
         var change = $(this).hasClass('addVote') ? 1 : -1;
         _vote.text((parseInt(_vote.text())+change));
     });
   });

检查此小提琴链接https://jsfiddle.net/36udn5Lv/2/

答案 1 :(得分:2)

请尝试以下代码段。

&#13;
&#13;
$(function() {
   $(".addVote").click(function() {
     $(this).parents('tr').find('td.vote').html(parseInt($(this).parents('tr').find('td.vote').html())+1);
   });
   $(".subVote").click(function() {
     $(this).parents('tr').find('td.vote').html(parseInt($(this).parents('tr').find('td.vote').html())-1);
   });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 
<table>
<th>Name</th>
<th>Vote</th>
<th>Action</th>

<tr>
   <td>User A</td>
   <td class="vote">5</td>
   <td>
     <a href="#" class="btn btn-lg btn-primary addVote">+</a>
     <a href="#" class="btn btn-lg btn-primary subVote">-</a>
   </td>
</tr>
<tr>
   <td>User B</td>
   <td class="vote">3</td>
   <td>
     <a href="#" class="btn btn-lg btn-primary addVote">+</a>
     <a href="#" class="btn btn-lg btn-primary subVote">-</a>
   </td>
</tr>
</table>
&#13;
&#13;
&#13;

答案 2 :(得分:2)

检查一下:

  public function postLogin(Request $request)
   {
     $this->validate($request, array('username' => 'required', 'password' => 'required'));
    $credentials = $request->only('USERNAME', 'PASSWORD');

    if (Auth::validate($credentials)) 
    {
        $user = Auth::getLastAttempted();
        Auth::login($user, $request->has('remember'));
        return redirect()->intended($this->redirectPath());
    }
    return redirect($this->loginPath())
    ->withInput($request->only('USERNAME', 'remember'))
    ->withErrors([
        'username' => $this->getFailedLoginMessage(),
    ]);

}
相关问题