有没有办法强制数字键盘出现在电话<input type="text">?我刚刚意识到,<input type="number">在HTML5中是“浮点数”,所以它不适合信用卡号,邮政编码等。

我想模拟<input type="number">的数字键盘功能,用于接受浮点数以外的数值的输入。是否有其他合适的输入类型可以做到这一点?


当前回答

<input type="text" inputmode="numeric">

使用输入模式,你可以给浏览器一个提示。

其他回答

使用<input type="text" pattern="\d*">打开数字键盘是有危险的。在firefox和chrome上,模式中包含的正则表达式会导致浏览器验证该表达式的输入。如果它与模式不匹配或为空,则会发生错误。注意其他浏览器中的意外操作。

试试这个:

$(document).ready(function() {
    $(document).find('input[type=number]').attr('type', 'tel');
});

参见:https://answers.laserfiche.com/questions/88002/Use-number-field-input-type-with-Field-Mask

<input type="text" inputmode="numeric">

使用输入模式,你可以给浏览器一个提示。

截至2020年

<input type="number" pattern="[0-9]*" inputmode="numeric">

CSS技巧做了一篇很好的文章:https://css-tricks.com/finger-friendly-numerical-inputs-with-inputmode/

2018年:

<input type="number" pattern="\d*">

Android和iOS都适用。

我在Android(^4.2)和iOS(11.3)上进行了测试