Is there a way to detect when a value in a number field is being increased or decreased? I would like that to determine wether or not to add or subtract an extra amount eg shit is down so +5 if increasing etc.
I have:
var oldVal = input.val();
input.change(function() {
var newVal = input.val();
if (newVal > oldVal) {
if (shiftIsDown) {
input.val(parseInt(input.val()) + 5);
}
}
if (newVal < oldVal) {
if (shiftIsDown) {
input.val(parseInt(input.val()) - 5);
}
}
oldVal = newVal;
})
But it behaves strangely and does'nt really work.
Try something like
var oldVal = input.val();
input.change(function(e) {
var newVal = input.val();
var shiftIsDown = e.shiftKey ? true : false;
if(shiftIsDown) {
if(newVal > oldVal)
input.val(parseInt(input.val()) + 5);
else if (newVal < oldVal)
input.val(parseInt(input.val()) - 5);
}
oldVal = newVal;
})
assuming input
is a jquery object.
Look at this Fiddle :
var input = $("#input");
var oldVal = input.val();
var newVal = input.val();
input.keyup(function () {
oldVal = newVal;
newVal = input.val();
});
$(document).keydown(function (e) {
if (e.keyCode == 16) {
if (newVal > oldVal) {
input.val(parseInt(input.val()) + 5);
}
if (newVal < oldVal) {
input.val(parseInt(input.val()) - 5);
}
}
});
keyup function is for listening of input changes
keydown functions is for shift down listening - (e.keyCode == 16)
is equals to pressing shift event, so when you ever press shift the input val is being changing
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.