简体   繁体   English

输入类型编号:如何检测值是递增还是递减?

[英]Input type number: how to detect if value was incremented or decremented?

Up until now, I simply used "change" to see if an input field of the type "number" was changed.到目前为止,我只是使用“change”来查看“number”类型的输入字段是否被更改。 However, now I need to know if the number was incremented or decremented to perform different actions.但是,现在我需要知道数字是增加还是减少以执行不同的操作。 How can I see how the number was changed?我怎么能看到号码是如何改变的?

Looking for solutions with JQuery, but plain old JavaScript is fine as well.寻找使用 JQuery 的解决方案,但普通的旧 JavaScript 也很好。

You could simply previously store the value of your input and compare it on change : 您可以简单地先存储输入值并在更改时进行比较:

 let value = $('#test').val(); $('#test').on('change',function(){ if($(this).val() > value){ console.log('Input was incremented'); }else{ console.log('Input was decremented'); } value = $(this).val(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" id="test" value="0"> 

You'll want to make use of a variable outside of your change function to keep track of the last value that was entered. 您需要使用change函数之外的变量来跟踪输入的最后一个值。 Inside of your change function, simply compare against this value to find out whether the new value is higher or lower. 在更改函数内部,只需与此值进行比较,即可确定新值是高还是低。 Don't forget to update the previous value after the check! 检查后不要忘记更新以前的值!

This can be seen in the following: 这可以在以下内容中看到:

 let previous_value; document.getElementById("input").addEventListener("change", function() { let value = document.getElementById("input").value; if (previous_value > value) { console.log("Decreased"); } else if (previous_value < value) { console.log("Increased"); } previous_value = value; }); 
 <input type="number" id="input"> 

Along with the suggestions made of storing the previous value in memory in the JS, you could also store it on the input element itself, as a data attribute. 除了在JS中将先前值存储在内存中的建议之外,您还可以将其作为数据属性存储在输入元素本身上。 That way JS from anywhere in your application will know the previous value, without having to have access to a variable 这样,来自应用程序中任何位置的JS都将知道以前的值,而无需访问变量

<input class="spinner" type="number" data-prev-value="0" />

$('.spinner').on('change', (e) => {
  let direction = e.target.value > parseInt(e.target.dataset.prevValue) ? 'up' : 'down'
  e.target.dataset.prevValue = e.target.value;
  console.log(direction);
})

Whilst the answers above do what the OP requested, it is also worth noting that the input field of type "number" can be changed by user input as well as by the arrows.虽然上面的答案符合 OP 的要求,但值得注意的是,“数字”类型的输入字段可以通过用户输入以及箭头进行更改。 Hence, although you know the direction, you will not neccessarily know how much the item has been incremented or decremented.因此,虽然您知道方向,但您不一定知道项目增加或减少了多少。 This function will show a positive or negative number (so doing what the OP requested) but also show the amount incremented or decremented:此函数将显示正数或负数(因此执行 OP 要求的操作),但还会显示增加或减少的数量:

let value = $('#test').val();

$('#test').on('change', function() {
    change = $(this).val() - value;
    value = $(this).val();

    console.log(change);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM