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

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


当前回答

试试这个:

$(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

其他回答

试试这个:

$(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

使用type="email"或type="url"至少在某些手机上可以给你一个键盘,比如iPhone。对于电话号码,你可以使用type="tel"。

截至2020年

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

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

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

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

有一个简单的方法来实现这种类型的行为(比如如果我们想在输入字段中使用文本格式,但仍然希望显示数字键盘):

我的输入:

<input inputMode=“numeric” onChange={handleInputChange} />

提示:如果你想要这种类型的行为(逗号分隔的数字) 然后遵循handleInputChange实现(这是基于上述状态的反应)