Jquery 如何加上或减去“a”;例如;根据原始值,只有一次?
我试图使每当我点击“喜欢”,程序增加1的价值。此外,如果我单击“不像”,它会从原始值中减去1。但只有一次 因为我可以喜欢一次,也可以不喜欢一次。。。(请注意,如果我取消了已喜欢的值的链接,它将从原始值中减去)Jquery 如何加上或减去“a”;例如;根据原始值,只有一次?,jquery,Jquery,我试图使每当我点击“喜欢”,程序增加1的价值。此外,如果我单击“不像”,它会从原始值中减去1。但只有一次 因为我可以喜欢一次,也可以不喜欢一次。。。(请注意,如果我取消了已喜欢的值的链接,它将从原始值中减去) var checker=true; $(“.like”)。单击(函数(){ 如果(检查){ var值=$(this.sibbines(“.total”).text(); var integer=parseInt(值) y=整数+1; var值=$(this).this(“.total”)
var checker=true;
$(“.like”)。单击(函数(){
如果(检查){
var值=$(this.sibbines(“.total”).text();
var integer=parseInt(值)
y=整数+1;
var值=$(this).this(“.total”).text(y);
checker=false;
}
});
$(“.nothing”)。单击(函数(){
如果(检查){
var值=$(this.sibbines(“.total”).text();
var integer=parseInt(值)
y=整数-1;
var值=$(this).this(“.total”).text(y);
checker=false;
}
});代码>
喜欢
5
不像
喜欢
12
不像
试试看:)
$(document).on('click','like',function(){
var attr=$(this.parent('.parent').attr('checked');
if(!(typeof attr!==typeof undefined&&attr!==false)){
var值=$(this.sibbines('.total').text();
var integer=parseInt(值);
++积分;
$(this).sides('.total').text(整数);
$(this).parent('.parent').attr('checked','checked');
}
});
$(文档).on('单击','与',函数()不同){
var attr=$(this.parent('.parent').attr('checked');
if(!(typeof attr!==typeof undefined&&attr!==false)){
var值=$(this.sibbines('.total').text();
var integer=parseInt(值);
--积分;
$(this).sides('.total').text(整数);
$(this).parent('.parent').attr('checked','checked');
}
});代码>
喜欢不像
5
喜欢不像
12
试试看
基本解决方案是在相应的块中保存每个的状态,如或。我使用了.parent
的数据
如果您希望在“喜欢”和“不喜欢”之后使其变得讨人喜欢,或者反之亦然,则必须将“相反”状态更改为false,例如。您可以通过将属性设置为相应的.total
字段来实现此目的
var checker=true;
$(“.like”)。单击(函数(){
if($(this).sides(“.total”).prop(“likeValue”)==1)
返回;
var值=$(this.sibbines(“.total”).text();
var integer=parseInt(值);
if($(this).sides(“.total”).prop(“likeValue”)==-1)
y=整数+2;
其他的
y=整数+1;
var值=$(this).this(“.total”).text(y);
$(this.this.“.total”).prop(“likeValue”,1);
});
$(“.nothing”)。单击(函数(){
if($(this).sides(“.total”).prop(“likeValue”)==-1)
返回;
var值=$(this.sibbines(“.total”).text();
var integer=parseInt(值)
if($(this).sides(“.total”).prop(“likeValue”)==1)
y=整数-2;
其他的
y=整数-1;
var值=$(this).this(“.total”).text(y);
$(this.this.“.total”).prop(“likeValue”,-1);
});代码>
喜欢
5
不像
喜欢
12
不像
如果您不喜欢该按钮,该按钮将被激活尼斯,但当我不喜欢一个已经喜欢的值时,它不会从该值中减去该值。问题是,即使您取消按钮的链接,我也希望它加上+1,但这必须做一次。。反之亦然,这是我最初的问题