如何在 Android 手机网站中强制键盘输入数字

我有一个手机网站,里面有一些 HTML input元素,像这样:

<input type="text" name="txtAccessoryCost" size="6" />

我已经把这个站点嵌入到了一个 WebView中,以便可能使用 Android 2.1,因此它也将是一个 Android 应用程序。

当这个 HTMLinput元素被聚焦时,是否可以得到带数字的键盘而不是带字母的默认键盘?

或者,如果对于 HTML 元素不可行,是否可以为整个应用程序(可能是 清单文件中的某些内容)设置它?

175313 次浏览
<input type="number" />
<input type="tel" />

Both of these present the numeric keypad when the input gains focus.

<input type="search" /> shows a normal keyboard with an extra search button

Everything else seems to bring up the standard keyboard.

This should work. But I have same problems on an Android phone.

<input type="number" /> <input type="tel" />

I found out, that if I didn't include the jquerymobile-framework, the keypad showed correctly on the two types of fields.

But I havn't found a solution to solve that problem, if you really need to use jquerymobile.

UPDATE: I found out, that if the form-tag is stored out of the

<div data-role="page">

The number keypad isn't shown. This must be a bug...

IMPORTANT NOTE

I am posting this as an answer, not a comment, as it is rather important info and it will attract more attention in this format.

As other fellows pointed, you can force a device to show you a numeric keyboard with type="number" / type="tel", but I must emphasize that you have to be extremely cautious with this.

If someone expects a number beginning with zeros, such as 000222, then she is likely to have trouble, as some browsers (desktop Chrome, for instance) will send to the server 222, which is not what she wants.

About type="tel" I can't say anything similar but personally I do not use it, as its behavior on different telephones can vary. I have confined myself to the simple pattern="[0-9]*" which do not work in Android

input type = number

When you want to provide a number input, you can use the HTML5 input type="number" attribute value.

<input type="number" name="n" />

Here is the keyboard that comes up on iPhone 4:

iPhone Screenshot of HTML5 input type number Android 2.2 uses this keyboard for type=number:

Android Screenshot of HTML5 input type number

Add a step attribute to the number input

<input type="number" step="0.01">

Source: http://blog.pamelafox.org/2012/05/triggering-numeric-keyboards-with-html5.html

Some browsers igoners sending leading zero to the server when the input type is "number". So I use a mixing of jquery and html to load a numeric keypad and also make sure that the value is sent as a text not as a number:

$(document).ready(function(){
$(".numberonly").focus(function(){$(this).attr("type","number")});
$(".numberonly").blur(function(){$(this).attr("type","text")});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="numberonly">

inputmode according to WHATWG spec is the the default method.

For iOS devices adding pattern could also help.

For backward compatibility use type as well since Chrome use these as of version 66.

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

For a Numeric Keyboard with DECIMAL POINT

<input inputmode="decimal" type="number" />

enter image description here