Jquery 单击+;和按钮
我在jQuery中有一个求和计算过程,如下所示:Jquery 单击+;和按钮,jquery,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
<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>
我想要实现的是,当我单击“+”按钮时,newVote值应该替换vote值,并且每次单击“+”按钮时,该过程都将继续。如果单击“-”按钮,则应执行相同的过程。我只能做一次。下一个过程什么也没发生。我怎样才能做到这一点呢?我希望这对你有用
$(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));
});
});
检查此小提琴链接我希望它对您有用
$(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));
});
});
检查此小提琴链接请尝试以下代码段
$(函数(){
$(“.addVote”)。单击(函数(){
$(this.parents('tr').find('td.vote').html(parseInt($(this.parents('tr').find('td.vote').html())+1);
});
$(“.subVote”)。单击(函数(){
$(this.parents('tr').find('td.vote').html(parseInt($(this.parents('tr').find('td.vote').html())-1);
});
});代码>
名称
投票
行动
用户A
5.
用户B
3.
请尝试以下代码片段
$(函数(){
$(“.addVote”)。单击(函数(){
$(this.parents('tr').find('td.vote').html(parseInt($(this.parents('tr').find('td.vote').html())+1);
});
$(“.subVote”)。单击(函数(){
$(this.parents('tr').find('td.vote').html(parseInt($(this.parents('tr').find('td.vote').html())-1);
});
});代码>
名称
投票
行动
用户A
5.
用户B
3.
检查以下内容:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
</head>
<body>
<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>
<script>
$(function() {
$(".addVote").click(function() {
var vote = $(this).closest('tr').find('td.vote').text();
var newvote = parseInt(vote)+parseInt(1);
$(this).closest('tr').find('td.vote').text(newvote);
});
$(".subVote").click(function() {
var vote = $(this).closest('tr').find('td.vote').text();
var newvote = parseInt(vote)-parseInt(1);
$(this).closest('tr').find('td.vote').text(newvote);
});
});
</script>
</body>
</html>
名称
投票
行动
用户A
5.
用户B
3.
$(函数(){
$(“.addVote”)。单击(函数(){
var-vote=$(this.closest('tr').find('td.vote').text();
var newvote=parseInt(投票)+parseInt(1);
$(this).closest('tr').find('td.vote').text(newvote);
});
$(“.subVote”)。单击(函数(){
var-vote=$(this.closest('tr').find('td.vote').text();
var newvote=parseInt(投票)-parseInt(1);
$(this).closest('tr').find('td.vote').text(newvote);
});
});
检查以下内容:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
</head>
<body>
<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>
<script>
$(function() {
$(".addVote").click(function() {
var vote = $(this).closest('tr').find('td.vote').text();
var newvote = parseInt(vote)+parseInt(1);
$(this).closest('tr').find('td.vote').text(newvote);
});
$(".subVote").click(function() {
var vote = $(this).closest('tr').find('td.vote').text();
var newvote = parseInt(vote)-parseInt(1);
$(this).closest('tr').find('td.vote').text(newvote);
});
});
</script>
</body>
</html>
名称
投票
行动
用户A
5.
用户B
3.
$(函数(){
$(“.addVote”)。单击(函数(){
var-vote=$(this.closest('tr').find('td.vote').text();
var newvote=parseInt(投票)+parseInt(1);
$(this).closest('tr').find('td.vote').text(newvote);
});
$(“.subVote”)。单击(函数(){
var-vote=$(this.closest('tr').find('td.vote').text();
var newvote=parseInt(投票)-parseInt(1);
$(this).nexture('tr').find('td.police').text(new投票);
});
});
它正在工作。谢谢你!所有的答案都很有效,很有效。谢谢你!所有答案都很好。我认为,这是一个优化的解决方案,执行负载较轻,然后是Roma的答案。所以,如果你喜欢这个答案,请检查它的权利。我认为,这是优化的解决方案,轻负载的执行然后罗马的答案。所以,如果你喜欢这个答案,请检查它的权利。