【问题标题】:input to behave as a number field for desktop and mobile devices输入以充当桌面和移动设备的数字字段
【发布时间】:2016-05-20 11:39:03
【问题描述】:

我在应用程序中有一个如下所示的输入字段,我已将其作为类型编号。用户可以点击上下来增加数字,也可以在字段中输入数值。

<input type="number" name="quantity" id="quantity" value="1" class="form-control" min="1">

但是当我使用移动设备时,同一个字段的行为就像一个文本字段。有什么我可以做的吗?

建议。

【问题讨论】:

标签: jquery html


【解决方案1】:

您可以使用jquery进行验证,通过使用下面的代码,用户甚至不能在文本框中拖放数字,只需将文本框的类名设为“numericOnly”即可。

$(function () {
    $(".numericOnly").bind('keypress', function (e) {
        if (e.keyCode == '9' || e.keyCode == '16') {
            return;
        }
        var code;
        if (e.keyCode) code = e.keyCode;
        else if (e.which) code = e.which;
        //if (e.which == 46)
        // return false;
        if (code == 8 || code == 46)
            return true;
        if (code < 48 || code > 57)
            return false;
    }
    );
    $(".numericOnly").bind("paste", function (e) {
        e.preventDefault();
    });
    $(".numericOnly").bind('mouseenter', function (e) {
        var val = $(this).val();
        if (val != '0') {
            val = val.replace(/[^0-9.]+/g, "")
            $(this).val(val);
        }
    });
});

【讨论】:

    猜你喜欢
    • 2020-08-22
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 2020-02-11
    • 1970-01-01
    相关资源
    最近更新 更多