javascript jQuery - 无法检索输入数字值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16689588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:38:54  来源:igfitidea点击:

jQuery - Cannot retrieve Input Number value

javascriptjqueryhtmlinput

提问by user2316341

<input id='nb' name="nb" type="number" placeholder="Number"/>

jQuery.val() returns the value if there are only numbers, but empty if there is any letter.

jQuery.val() 如果只有数字则返回值,如果有字母则返回空。

Chrome shows the input number arrows (and they work), but I can type letters too.. Weird thing

Chrome 显示输入数字箭头(并且它们工作),但我也可以输入字母......奇怪的事情

BTW on mozilla and IE8 it becomes a normal input text (guess I have an old mozilla)

顺便说一句,在 mozilla 和 IE8 上它变成了一个普通的输入文本(我猜我有一个旧的 mozilla)

Any idea ? Couldn't find anything on jQuery doc specific to number inputs with .val()

任何的想法 ?在 jQuery 文档上找不到任何特定于使用 .val() 的数字输入的内容

回答by DaGLiMiOuX

Suggestion for input type="text"instead HTML5 input input type="number":

建议input type="text"改为 HTML5 输入input type="number"

//HTML

<input id="inp" type="text" />

//JAVASCRIPT (JQUERY NEEDED)

function onlyNumbers(evt) {
    // SOME OPTIONS LIKE ENTER, BACKSPACE, HOME, END, ARROWS, ETC.
    var arrayExceptions = [8, 13, 16, 17, 18, 20, 27, 35, 36, 37,
        38, 39, 40, 45, 46, 144];
    if ((evt.keyCode < 48 || evt.keyCode > 57) &&
            (evt.keyCode < 96 || evt.keyCode > 106) && // NUMPAD
            $.inArray(evt.keyCode, arrayExceptions) === -1) {
        return false;
    }
}

$('#inp').on('keydown', onlyNumbers);

//JAVASCRIPT (WITHOUT JQUERY)

function inArray(value, arr) {
    for(var i = 0; i < arr.length; i++) {
        if (value === arr[i]) {
            return i;
        }
    }
    return -1;
}

function onlyNumbers(evt) {
    // SOME OPTIONS LIKE ENTER, BACKSPACE, HOME, END, ARROWS, ETC.
    var arrayExceptions = [8, 13, 16, 17, 18, 20, 27, 35, 36, 37,
        38, 39, 40, 45, 46, 144];
    if ((evt.keyCode < 48 || evt.keyCode > 57) &&
            (evt.keyCode < 96 || evt.keyCode > 106) && // NUMPAD
              inArray(evt.keyCode, arrayExceptions) === -1) {
        return false;
    }
}

document.getElementById('inp').onkeydown = onlyNumbers;

As I said in my comment, until HTML5 becomes an standard web language that we can work with it properly, I prefer to use this.

正如我在评论中所说,在 HTML5 成为我们可以正确使用它的标准 Web 语言之前,我更喜欢使用它。

Demowith jQuery
Demowithout jQuery

演示使用jQuery
演示没有的jQuery

回答by Arslan Rafique

If you want to get only numbers from input then you need to use regular expressions. Initially get the value from input using $(your_element).val() then use regular expressions.

如果您只想从输入中获取数字,则需要使用正则表达式。最初使用 $(your_element).val() 从输入中获取值,然后使用正则表达式。

Hereis a running example. Hope it helps.

是一个运行示例。希望能帮助到你。