jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了javascript – 通过上/下箭头键盘输入文本中的Jquery增加/减少数字大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个基本数量字段,并希望允许用户根据键盘向上/向下增加/减少此输入框中的数字.

继续之后:关于键盘代码https://stackoverflow.com/a/375426/560287的EndangeredMassa答案如何将其添加键盘功能中?

var keynum = 0;

if(window.event) { keynum = e.keyCode; }  // IE (sucks)
else if(e.which) { keynum = e.which; }    // netscape/Firefox/Opera

if(keynum == 38) { // up
    //Move SELEction up
}

if(keynum == 27) { // down
    //Move SELEction down
}

解决方法

//cache our input since we will be working with it each time an arrow key is pressed
var $input = $('input');

//bind the the `keydown` event for the `document` object which will catch all `keydown` events that bubble up the DOM
$(document).on('keydown',function (event) {

    //up-arrow (regular and num-pad)
    if (event.which == 38 || event.which == 104) {

        //make sure to use `parseInt()` so you can numerically add to the value rather than concocTing a longer @R_801_10495@ng
        $input.val((parseInt($input.val()) + 1));

    //down-arrow (regular and num-pad)
    } else if (event.which == 40 || event.which == 98) {
        $input.val((parseInt($input.val()) - 1));
    }
});

这是一个演示:http://jsfiddle.net/QRNP8/1/

请注意,jQuery将charCode / keyCode属性规范化为event.which:

资料来源:http://api.jquery.com/category/events/event-object/

大佬总结

以上是大佬教程为你收集整理的javascript – 通过上/下箭头键盘输入文本中的Jquery增加/减少数字全部内容,希望文章能够帮你解决javascript – 通过上/下箭头键盘输入文本中的Jquery增加/减少数字所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。