Javascript 如何增加按钮上的数字值单击jquery
html 我正在获取id变量中的like数量,但不知道如何在成功时增加它我在代码中尝试了它,但不起作用它在整数前面加1有什么帮助吗只要替换这个:Javascript 如何增加按钮上的数字值单击jquery,javascript,php,jquery,Javascript,Php,Jquery,html 我正在获取id变量中的like数量,但不知道如何在成功时增加它我在代码中尝试了它,但不起作用它在整数前面加1有什么帮助吗只要替换这个: $(document).ready(function(){ // when the user clicks on like $(document).on('click','.like',function(){ $('.like').addClass('animated bounceIn'); var posti
$(document).ready(function(){
// when the user clicks on like
$(document).on('click','.like',function(){
$('.like').addClass('animated bounceIn');
var postid = $(this).data('id');
var id = $(this).attr('data-likes');
$post = $(this);
$.ajax({
url: 'dashboard.php',
type: 'POST',
data: {
'liked': 1,
'postid': postid
},
success: function(response){
$post.parent().find('span.likes_count').text(id+1+"likes");
$post.addClass('hide');
$post.siblings().removeClass('hide');
}
});
});
与:
问题是id
,以及“likes”
都是字符串,因此Javascript将+
操作符解释为在字符串之间进行串联。“一元加号”+id
将其转换为一个数字(您也可以使用number(id)
,有些人认为它更明确,但我个人认为它很冗长),通过将+id+1
放在括号中,您可以确保在字符串与“likes”连接之前将其视为数字加法
会发生。只需替换此:
$(document).ready(function(){
// when the user clicks on like
$(document).on('click','.like',function(){
$('.like').addClass('animated bounceIn');
var postid = $(this).data('id');
var id = $(this).attr('data-likes');
$post = $(this);
$.ajax({
url: 'dashboard.php',
type: 'POST',
data: {
'liked': 1,
'postid': postid
},
success: function(response){
$post.parent().find('span.likes_count').text(id+1+"likes");
$post.addClass('hide');
$post.siblings().removeClass('hide');
}
});
});
与:
问题是
id
,以及“likes”
都是字符串,因此Javascript将+
操作符解释为在字符串之间进行串联。“一元加号”+id
将其转换为一个数字(您也可以使用number(id)
,有些人认为它更明确,但我个人认为它很冗长),通过将+id+1
放在括号中,您可以确保在字符串与“likes”连接之前将其视为数字加法
发生。您可以添加任何实时演示吗?使用HTML代码?…text(parseInt(id)+1+“likes”)
您可以添加任何实时演示吗?使用HTML代码?…文本(parseInt(id)+1+“likes”)
$post.parent().find('span.likes_count').text(id+1+"likes");
$post.parent().find('span.likes_count').text((+id+1)+"likes");