到目前为止,我只是简单地使用“更改”来查看“数字”类型的输入字段是否已更改。但是,现在我需要知道数字是递增还是递减以执行不同的操作。我怎样才能看到号码是如何更改的?
寻找 JQuery 的解决方案,但普通的旧 JavaScript 也可以。
最佳答案
您可以简单地预先存储您输入的值并在更改时比较它:
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">
关于javascript - 输入类型编号 : how to detect if value was incremented or decremented?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49971457/