jQuery通过键盘的上下箭头增减输入文本中的数字

3

我有一个基本的数量字段,希望用户可以通过键盘上下箭头在输入框中增加/减少数字。

继承自:EndangeredMassa在键盘代码https://dev59.com/MHVC5IYBdhLWcg3weBE-#375426的答案,我该如何将其添加到keyup函数中?

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
}

1
你看过tangle.js吗?它有一种非常好的处理数字字段的方式。 - CamelCamelCamel
我认为你现在所需的是this.value++和this.value--分别用于上箭头和下箭头,你可能需要使用this.value = this.value.toInt()++。 - CBusBus
8个回答

7
//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 string
        $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属性:

Query normalizes the following properties for cross-browser consistency:

target
relatedTarget
pageX
pageY
which
metaKey

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

这是一个关于jQuery事件对象的文档。事件对象是在DOM元素上触发事件时自动创建的JavaScript对象。它包含有关事件的详细信息,例如哪个键被按下或释放,鼠标指针的位置以及由事件调用的处理程序。您可以使用事件对象来编写更具交互性和可访问性的网页应用程序。

停止传播使用"=="的做法! :P - jbabey
@jbabey 实际上,我很高兴有人提出这个问题,那么改进在哪里呢?我知道三个等号要求变量类型必须相同,但它们的执行效果是一样的...:http://jsperf.com/double-equals-vs-tripple-equals - Jasper
使用身份运算符而不是等式运算符,理论上至少可以迫使你编写更好的代码:P我会将其等同于在VB.net/C#中打开选项严格模式 - 这是一个良好的实践,无论是否可以使用另一种方式,都应该始终使用它,以避免那些让你感到困扰的边缘情况。 - jbabey
我知道这个问题很老了,但是我想知道如何只在输入字段当前被选中时才使其工作?目前,当有人使用箭头滚动页面时,它会更改输入字段。 编辑:没关系,我一问就明白了!我用$('input').focus(function() { // the rest here });将其包装起来。 - Benbodhi

5

将输入类型设置为数字也可以起作用。但是在IE9及以下版本中效果不太好。

<input type="number">

来源:http://www.w3schools.com/html/tryit.asp?filename=tryhtml_input_number

这是一个HTML示例,用于演示如何使用“input”元素的“type = number”特性。此特性允许用户输入数字值,并可以在代码中进行验证和操作。如果您需要在网页中使用数字输入字段,这个示例将对您有所帮助。

2

1

你可以这样做:

<input type="text" id="yourinput" value="0">

$(document).on("keypress", '*', function(e) {
    if (e.keyCode == 38) { // up
        $('#yourinput').val(parseInt($('#yourinput').val(), 10) + 1);
    }

    if (e.keyCode == 40) { // down
        $('#yourinput').val(parseInt($('#yourinput').val(), 10) + 1);
    }
});

在这里调试 http://jsfiddle.net/mSCBL/1/


我认为你想要写的是: 如果 (e.keyCode == 40) { // 向下 $('#yourinput').val(parseInt($('#yourinput').val(), 10) - 1); } 而不是: 如果 (e.keyCode == 40) { // 向下 $('#yourinput').val(parseInt($('#yourinput').val(), 10) + 1); } - Sajib Mahmood

1
$("input").keypress(function(event) {
      var val=$(this).val();
      if ( event.keyCode== 38) {
          val++
         $(this).val(val)
      }
      if ( event.keyCode== 40) {
          val--
          $(this).val(val)
      };    
});

0
$("something").keyup(function(e){
    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
    }
});

something 是一个选择器,用于匹配您的输入。


jQuery 规范化了 which 属性,使其可以跨浏览器工作。我认为。 - Nicola Peluchetti
好的。我并没有真正看代码本身,只是展示如何将它放入“keyup”中。 - James Montagne

0

你的代码看起来差不多正确。如果你只是想知道如何将代码绑定到事件上...

$('#itemId').keyup(function(e){ 
    /*YOUR CODE*/  
});

0

这应该可以运行

if(keynum == 38) { // up
    this.value = parseInt(this.value)-1;
}

if(keynum == 27) { // down
    this.value = parseInt(this.value)+1;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接