柜台小问题(不顺利)
Counter small issue (does not work smoothly)
在下面的这个 jsfiddle 中,您会注意到递增或递减 (?) 不能很好地工作 - 它会关闭一个数字(向上或向下) - 我正在寻找一种使其完美的方法。
http://jsfiddle.net/Sergelie/8d3th1cb/3/
<div data-role="page">
<div data-role="content">
<input id="button" type="button" value="+" />
<input id="button2" type="button" value="-" />
</div>
</div>
想法是从 0 向上到无限,向下到 0 停止(而不是像现在那样到 -1)。
var count = 1;
$("#button").on('click', function () {
$(this).val(count++).button("refresh");
});
$("#button2").on('click', function () {
if (count>-1)
$("#button").val(count--).button("refresh");
});
您可以使用前缀运算符 (++count/--count) 代替(将计数初始化为 0):
var count = 0;
$("#button").on('click', function() {
$(this).val(++count).button("refresh");
});
$("#button2").on('click', function() {
if (count > 0)
$("#button").val(--count).button("refresh");
});
改用++count
和--count
,这样之前的值为incremented/decremented,表达式的值为最终值:
var count = 1;
$("#button").on('click', function() {
$(this).val(++count).button("refresh");
});
$("#button2").on('click', function() {
if (count > 0) $("#button").val(--count).button("refresh");
});
另请参阅:++someVariable Vs. someVariable++ in Javascript
var count = 0;
$("#button").on('click', function () {
$(this).val(++count).button("refresh");
});
$("#button2").on('click', function () {
if (count>0)
$("#button").val(--count).button("refresh");
});
了解递增和递减运算符的位置here。
在下面的这个 jsfiddle 中,您会注意到递增或递减 (?) 不能很好地工作 - 它会关闭一个数字(向上或向下) - 我正在寻找一种使其完美的方法。
http://jsfiddle.net/Sergelie/8d3th1cb/3/
<div data-role="page">
<div data-role="content">
<input id="button" type="button" value="+" />
<input id="button2" type="button" value="-" />
</div>
</div>
想法是从 0 向上到无限,向下到 0 停止(而不是像现在那样到 -1)。
var count = 1;
$("#button").on('click', function () {
$(this).val(count++).button("refresh");
});
$("#button2").on('click', function () {
if (count>-1)
$("#button").val(count--).button("refresh");
});
您可以使用前缀运算符 (++count/--count) 代替(将计数初始化为 0):
var count = 0;
$("#button").on('click', function() {
$(this).val(++count).button("refresh");
});
$("#button2").on('click', function() {
if (count > 0)
$("#button").val(--count).button("refresh");
});
改用++count
和--count
,这样之前的值为incremented/decremented,表达式的值为最终值:
var count = 1;
$("#button").on('click', function() {
$(this).val(++count).button("refresh");
});
$("#button2").on('click', function() {
if (count > 0) $("#button").val(--count).button("refresh");
});
另请参阅:++someVariable Vs. someVariable++ in Javascript
var count = 0;
$("#button").on('click', function () {
$(this).val(++count).button("refresh");
});
$("#button2").on('click', function () {
if (count>0)
$("#button").val(--count).button("refresh");
});
了解递增和递减运算符的位置here。