输入类型编号:如何检测值是增加还是减少? [英] Input type number: how to detect if value was incremented or decremented?
本文介绍了输入类型编号:如何检测值是增加还是减少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
到目前为止,我仅使用"change"来查看类型为"number"的输入字段是否已更改.但是,现在我需要知道数字是增加还是减少以执行不同的操作.我怎么知道号码是怎么改变的?
Up until now, I simply used "change" to see if an input field of the type "number" was changed. 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?
使用JQuery寻找解决方案,但普通的旧JavaScript也可以.
Looking for solutions with JQuery, but plain old JavaScript is fine as well.
推荐答案
您可以简单地先前存储输入的值并在更改时进行比较:
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">
这篇关于输入类型编号:如何检测值是增加还是减少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文