[英]Javascript, increment a counter on button click
在javascript中,我要创建一个计数器,该计数器可以在您单击按钮时增加该值。
当我第一次单击添加按钮时,该数目不会增加。
但是,当我将值打印到控制台时,结果会增加。
小提琴: http : //jsfiddle.net/techydude/H63As/
$(function() {
var //valueCount = $("counter").value(),
counter = $("#counter"),
addBtn = $("#add"),
value = $("#counter").html();
addBtn.on("click", function() {
counter.html(value ++); //this value is not incremented.
console.log(value); //this value gets incremented.
return
});
});
如何使两行的值显示相同?
你的意思是:
addBtn.on("click", function() {
counter.html(++value);
return;
});
采用
value = parseInt($("#counter").html());
$(function() {
var //valueCount = $("counter").value(),
counter = $("#counter"),
addBtn = $("#add"),
value = parseInt($("#counter").html());
addBtn.on("click", function() {
counter.html(++value );
console.log(value);
return
});
});
addBtn.on("click", function() {
counter.html(++value);
console.log(value);
return
});
// Increment operators
x = 1;
y = ++x; // x is now 2, y is also 2
y = x++; // x is now 3, y is 2
// Decrement operators
x = 3;
y = x--; // x is now 2, y is 3
y = --x; // x is now 1, y is also 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.