解决小问题(工作不顺利)

Counter small issue (does not work smoothly)

本文关键字:不顺利 工作 问题 解决      更新时间:2024-04-24

在下面的这个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");
});

您可以使用前缀运算符(++计数/--计数)(将计数初始化为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");
});

jsFiddle示例

改为使用++count--count,使值在之前递增/递减,并且表达式的值为最终值:

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与Javascript 中的someVariable++

var count = 0;
$("#button").on('click', function () {
    $(this).val(++count).button("refresh");
});
$("#button2").on('click', function () {
    if (count>0)
        $("#button").val(--count).button("refresh");
});

请在此处阅读增量和减量运算符的位置。