简体   繁体   English

<input type=“number”/>未在 iOS 上显示数字键盘

[英]<input type=“number”/> is not showing a number keypad on iOS

According to Apple's documentation when I setup an input element with a type of number I should get a number keypad.根据Apple 的文档,当我设置带有number类型的输入元素时,我应该得到一个数字键盘。

<input type="number"/>

number : A text field for specifying a number. number :用于指定数字的文本字段。 Brings up a number pad keyboard in iOS 3.1 and later.在 iOS 3.1 及更高版本中调出数字键盘。

Looks damn near impossible to mess up.看起来该死的几乎不可能搞砸。 However, when I view this simple fiddle on my iPhone or the simulator (both iOS6) the number keypad does not appear, and I get the standard alphabetic keyboard instead.但是,当我在我的 iPhone 或模拟器(都是 iOS6)上查看这个简单的小提琴时,数字键盘没有出现,而是使用标准的字母键盘。

http://jsfiddle.net/Cy4aC/3/ http://jsfiddle.net/Cy4aC/3/

What on earth could I possibly have messed up here?我到底可能在这里搞砸了什么?

在此处输入图片说明

You need to specify the pattern:您需要指定模式:

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

As a number can be negative or with floating point, so the - and .由于number可以为负数或浮点数,因此-. and , should be available in the keyboard, unless you specify a digits only pattern.,应该在键盘中可用,除非您指定仅数字模式。

在此处输入图片说明

As of ios 12.2 (released March 25, 2019) this solution will work and it is the simplest to implement从 ios 12.2(2019 年 3 月 25 日发布)开始,此解决方案将起作用,并且实现起来最简单

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

This will bring up only the number key pad without the #+* characters that come with using input="tel"这将仅显示数字键盘,而没有使用 input="tel" 附带的 #+* 字符

我不确定这是否是您想要的,但输入 type = "tel" 会给您“电话号码键盘”。

In my experience, this is very inconsistent across browsers.根据我的经验,这在浏览器之间非常不一致。 iOS has gone back and forth between supporting this correctly for my use case.对于我的用例,iOS 在正确支持它之间来回切换。 I generally just want the default displayed keyboard to be the numeric one.我通常只希望默认显示的键盘是数字键盘。 The last time I checked, using input type="number" correctly brings up the numeric keyboard, but the "size" attribute is ignored, which throws off my mobile format badly.我上次检查时,使用 input type="number" 正确调出数字键盘,但忽略了“size”属性,这严重影响了我的移动格式。 I added an attribute to all inputs that I'd prefer the numeric keyboard to use by default, and I used jQuery to change any attributes (ie type="number") when the browser detects as one that works correctly.我为所有我希望默认使用数字键盘的输入添加了一个属性,并且当浏览器检测到可以正常工作时,我使用 jQuery 更改任何属性(即 type="number")。 That way I don't have to go back through and update the individual inputs and allows me to only apply it in supported browsers.这样我就不必返回并更新各个输入,并允许我只在支持的浏览器中应用它。

It would be lovely if the major mobile O/S's would have an attribute for "default keyboard" aside from the input type.如果主要的移动操作系统除了输入类型之外还有一个“默认键盘”的属性,那就太好了。 What if a user is entering an address or zip code?如果用户输入地址或邮政编码怎么办? Generally those start with numbers, so showing the number keyboard by default, but allowing ANY text, is helpful.通常那些以数字开头,因此默认情况下显示数字键盘,但允许任何文本,是有帮助的。

As far as validation, I can't rely on the browser to support validation for the specific input types, so I use javascript and server-side validation.至于验证,我不能依赖浏览器来支持特定输入类型的验证,所以我使用 javascript 和服务器端验证。

Greetings.问候。 I know this question is old but I felt it's a very sought after solution and decided to post an answer.我知道这个问题很老,但我觉得这是一个非常受欢迎的解决方案,并决定发布答案。

Here's a solution I've created to address the iPad keyboard as well as pretty much anything.这是我为解决 iPad 键盘以及几乎所有问题而创建的解决方案

Also, this approach does not require you to use input of type number which in my humble opinion is very ugly.此外,这种方法不需要您使用数字类型的输入,在我看来这是非常难看的。

Below, you may find a working version.在下面,您可能会找到一个工作版本。

Thoughts on Apple iPad keyboard keys...对 Apple iPad 键盘按键的思考...

I had to create a keyPress event handler to capture and suppress keys on the iPad that don't seem to be handled by the keyDown event or are ambiguous???!我必须创建一个 keyPress 事件处理程序来捕获和抑制 iPad 上似乎没有由 keyDown 事件处理或不明确的键???!

 // Bind keydown event to all input type=text in form. $("form input[type=text]").keydown(function (e) { // Reference to keyCodes... var key = e.which || e.keyCode; // Reference to shiftKey... var shift_key = e.shiftKey; // Reference to ctrlKey... var ctrl_key = e.ctrlKey; // Reference to altKey... var alt_key = e.altKey; // When user presses the shiftKey... if(e.shiftKey) { //...supress its click and whatever key follows after it. console.log(e.shiftKey + ' and ' + key + ' supressed.'); // Deny return false; // ONLY Allow Numbers, Numberpad, Backspace & Tab } else if ( (key >= 48 && key <=57) || (key >= 96 && key <=105) || (key >= 8 && key <=9) ) { // Allow return true; } else { // Deny every other key and/or shift + key combination NOT explicitly declared. return false; } }); // KeyPresss to handle remaining iPAD keyboard keys! // These keys don't seem to be handled by the keyDown event or are ambiguous???! // Bind keypress event to all input type=text in form. $("form input[type=text]").keypress(function (e) { // Reference to keyCodes... var key = e.which || e.keyCode; // Reference to shiftKey... var shift_key = e.shiftKey; // Reference to ctrlKey... var ctrl_key = e.ctrlKey; // Reference to altKey... var alt_key = e.altKey; switch (key) { // Character -> ^ case 94: return false; // Character -> # case 35: return false; // Character -> $ case 36: return false; // Character -> @ case 64: return false; // Character -> & case 38: return false; // Character -> * case 42: return false; // Character -> ( case 40: return false; // Character -> ) case 41: return false; // Character -> % case 37: return false; // Character -> ! case 33: return false; } });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action=""> <label for="text1">Numbers, Numberpad, Backspace & Tab ONLY:</label> <input id="text1" type="text" placeholder="0" min="0" input pattern="[0-9]*" inputmode="numeric" value="" maxlength="4"> </form>

After reading and trying a lot of ideas, none of what I found worked to show exactly the keyboard with digits only and the coma or dot.在阅读并尝试了很多想法之后,我发现没有一个能够准确显示仅包含数字和昏迷或点的键盘。

I finished using just <input type="number"> and a onkeyup handler on this input where I do something like this :我在这个输入上只使用了<input type="number">和一个onkeyup处理程序,我在那里做这样的事情:

var previousValue = localStorage.getItem('myInputId');
if (input.getAttribute('type') === "number" && input.validity && input.validity.badInput) {
    input.value = previousValue || "";
}
localStorage.setItem('myInputId', input.value);

This prevents the user to write wrong characters by replacing the value with the previous one if there is a validity error (I dont't know if this is an iOS object or standard)如果存在有效性错误,这可以防止用户通过将值替换为前一个值来编写错误的字符(我不知道这是 iOS 对象还是标准)

beware I did't test this code snippet because I have more complex stuff around on my side but I hope you will get the idea当心我没有测试这个代码片段,因为我身边有更复杂的东西,但我希望你能明白

With this solution :有了这个解决方案:

  • On iOS : the user has a full classic keyboard but opened by default on the numbers and can't write wrong characters.在 iOS 上:用户有一个完整的经典键盘,但默认情况下在数字上打开并且不能写错字符。
  • On Android, the number keyboard is perfect.在 Android 上,数字键盘是完美的。
  • In the future we can hope that iOS will trigger the good keyboard for numbers and that the javascript part will never be called.将来我们可以希望 iOS 会触发良好的数字键盘,并且永远不会调用 javascript 部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ios phonegap数字键盘 - ios phonegap number keypad iOS如何捕获将标准键盘类型切换为数字键盘的事件? - iOS how to catch event of switching standard keyboard type to number keypad? 在数字键盘上显示丢弃和完成按钮 - Showing discard & done buttons on number keypad ios9中缺少键的数字小键盘 - Number keypad with missing keys in ios9 移动Safari - 如何默认为十进制输入的完整数字小键盘w / o type =“number”,或禁用Safari输入类型=“数字”更正 - Mobile Safari - How to Default to Full Numeric Keypad for Decimal Entry w/o type=“number”, or disable Safari input type=“number” correction 如何实现IOS数字键盘,即类似于android中的数字键盘? - How to achieve IOS number keypad i.e similar to number keypad in android? 输入类型编号在iOS中不接受小数 - Input type number does not accept decimal in iOS 支持iOS设备(iPad)中的输入类型编号 - Support for input type number in iOS devices (iPad) Flutter 在使用文本输入时仅显示 0-9 键盘而不是扩展数字键盘(即所有 ascii 字符都应该可用) - Flutter showing only 0-9 keypad instead of extended number keypad when using text input (i.e. all ascii characters should be available) 将电话号码复制/粘贴到键盘中或以编程方式在iOS中拨打免费电话号码 - Copy/paste phone number into keypad or dial a toll free number in iOS programatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM