热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iPhone/iOS:为邮政编码提供HTML5键盘-iPhone/iOS:PresentingHTML5KeyboardforPostalCodes

ThereareseveraltricksfordisplayingdifferentkeyboardsonmobiledevicesforHTML5inputs(i.e

There are several tricks for displaying different keyboards on mobile devices for HTML 5 inputs (i.e. tags).

在移动设备上显示HTML 5输入的不同键盘有几种技巧(例如标签)。

For example, some are documented on Apple's website, Configuring the Keyboard for Web Views.

例如,苹果的网站上就记录了其中的一些功能,比如为Web view配置键盘。

enter image description here enter image description here

These are great for usability, but when it comes to an input for for international postal codes (mostly numeric, but letters allowed), we're left with some poor options. Most people recommend using the pattern="\d*" trick to show the numeric keyboard, but that doesn't allow for letter input.

这些对于可用性来说是很好的,但是当涉及到国际邮政编码的输入时(大部分是数字,但允许字母),我们会有一些糟糕的选择。大多数人推荐使用模式="\d*"技巧来显示数字键盘,但不允许输入字母。

The type="number" input type shows the regular keyboard but shifted to the numeric layout:

类型=“数字”输入类型显示常规键盘,但移动到数字布局:

enter image description here

This works well for iOS devices, but it makes Chrome think the input must be a number and even changes the input's behavior (up/down increment and decrement the value).

这在iOS设备上运行良好,但它让Chrome认为输入必须是一个数字,甚至会改变输入的行为(向上/向下递增和递减值)。

enter image description here

Is there any way to get iOS to default to the numeric layout, but still allow for alphanumeric input?

有没有办法让iOS默认设置为数字布局,但仍然允许输入字母数字?

Basically, I want the iOS behavior for type="number" but I want the field to behave like a regular text field on desktop browsers. Is this possible?

基本上,我希望type="number"的iOS行为,但我希望字段在桌面浏览器上表现得像一个常规文本字段。这是可能的吗?

UPDATE:

更新:

Sniffing the user-agent for iOS and using the type="number" input type is not an option. type="number" is not meant for string values (like postal codes), and it has other side effects (like stripping leading zeros, comma delimiters, etc) that make it less than ideal for postal codes.

嗅探iOS的用户代理并使用type="number"输入类型不是一个选项。type="number"并不表示字符串值(如邮政编码),它还有其他副作用(如去掉前导0、逗号分隔符等),这使得它对邮政编码不太理想。

7 个解决方案

#1


26  

Will this work?

这工作吗?

HTML:

HTML:


This should give you the nice numeric keyboard on Android/iOS phone browsers, disable browser form validation on desktop browsers, not show any arrow spinners, allows leading zeros, and allows commas and letters on desktop browsers, as well as on iPad.

这将使您在Android/iOS手机浏览器上拥有漂亮的数字键盘,在桌面浏览器上禁用浏览器表单验证,不显示任何箭头旋转器,允许前置零,允许在桌面浏览器和iPad上使用逗号和字母。

Android / iOS phones:

Android / iOS手机:

enter image description here

Desktop:

桌面:

enter image description here

iPad:

iPad:

enter image description here

#2


7  

A quick google search found this Stackoverflow question.

快速搜索谷歌找到了这个Stackoverflow问题。

HTML

HTML


Javascript

Javascript

$('input[type="text"]').on('touchstart', function() {
   $(this).attr('type', 'number');
});

$('input[type="text"]').on('keydown blur', function() {
   $(this).attr('type', 'text');
});

The input type is switched before the form can be validated, showing the correct keyboard without messing up the value. If you only want it to run on iOS, you will probably have to use the user agent.

在对表单进行验证之前,将切换输入类型,显示正确的键盘,而不会打乱值。如果您只想让它在iOS上运行,您可能需要使用用户代理。

Stackoverflow on detecting iOS

Stackoverflow iOS检测

#3


7  

Browsers currently have no proper way of representing numeric codes like postcodes and credit card numbers. The best solution is to use type='tel' which will give you a number keypad and ability to add any character on desktop.

浏览器目前没有合适的方式来表示数字代码,比如邮编和信用卡号。最好的解决方案是使用type='tel',它将给你一个数字键盘和能力添加任何字符在桌面上。

Type text and pattern='/d' will give you a number keypad but only on iOS.

输入文本和模式='/d'将给你一个数字键盘,但只在iOS上。

There is an HTML5.1 proposal for an attribute called inputmode which would allow you to specify keypad regardless of type. However not is not currently supported by any browser.

存在一个名为inputmode的HTML5.1建议属性,允许您指定键盘(无论类型如何)。然而,目前没有任何浏览器支持。

I would also recommend having a look at the Webshim polyfill library which has a polyfill method for these types of inputs.

我还建议您看看Webshim polyfill库,该库为这些类型的输入提供了一个polyfill方法。

#4


5  

This will make the numerical side of the ios keyboard display by default and maintains the ability to switch to the alphabetical side. When the html input type changes, the device changes the keyboard to match the appropriate type.

这将使ios键盘的数字端默认显示,并保持切换到字母端的能力。当html输入类型改变时,设备会改变键盘以匹配相应的类型。

(function ($) {
    var cOntrol= $('#test2');
    var field = $('#test1');

    control.bind('click', function () {
        if (control.is(':checked')) {
            field.attr('type', 'text');
        } else {
            field.attr('type', 'number');
        }
    })
}(jQuery));


Change

enter image description here


alternate demo: http://jsfiddle.net/davidcondrey/dbg1L0c0/3/embedded/result/

交替演示:http://jsfiddle.net/davidcondrey/dbg1L0c0/3/embedded/result/

If you want the large numerical format keyboard (the telephone style) you can adjust the code accordingly and it still works:

如果你想要大的数字格式键盘(电话风格),你可以相应地调整代码,它仍然可以工作:

(function ($) {
    var cOntrol= $('#test2');
    var field = $('#test1');

    control.bind('click', function () {
        if (control.is(':checked')) {
            field.attr('type', 'text');
        } else {
            field.attr('type', 'tel');
        }
    })
}(jQuery));


Change

enter image description here

#5


0  

Try this:

试试这个:




I know this is very hackish way, but this apparently works.
I haven't tested on Android devices though.

我知道这是非常陈腐的做法,但这显然行得通。我还没有在Android设备上测试过。

Note this may causes a little noticeable glitches when $this.attr('type', 'number') because this reset the value when input has non numerical characters.

请注意,这可能会引起一些明显的小故障。attr('type', 'number')因为当输入具有非数值字符时,会重置值。

Basic ideas are stolen from this answer :)

基本的想法从这个答案中被窃取:)

#6


0  

An update to this question in iOS 11. You can get the number keypad by simply adding the pattern attribute (pattern="[0-9]*") to any input with a number type.

在ios11中更新这个问题。只需将pattern属性(pattern="[0-9]*")添加到任何具有number类型的输入中,就可以获得number keypad。

The following works as expected.

以下是预期的工作。


This also works.

这同样适用。


@davidelrizzo posted part of the answer, but the comments from @Miguel Guardo and @turibe give a fuller picture but are easy to miss.

@davidelrizzo发布了部分答案,但是来自@Miguel Guardo和@turibe的评论提供了更完整的图片,但是很容易被忽略。

#7


-1  

Why not check the header of the HTTP request (user agent), and serve up the numeric layout to the iOS devices, while serving up the alphanumeric layout to the rest?

为什么不检查HTTP请求(用户代理)的头,将数字布局提供给iOS设备,而将字母数字布局提供给其他设备呢?


推荐阅读
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 使用在线工具jsonschema2pojo根据json生成java对象
    本文介绍了使用在线工具jsonschema2pojo根据json生成java对象的方法。通过该工具,用户只需将json字符串复制到输入框中,即可自动将其转换成java对象。该工具还能解析列表式的json数据,并将嵌套在内层的对象也解析出来。本文以请求github的api为例,展示了使用该工具的步骤和效果。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • 本文由编程笔记小编整理,主要介绍了使用Junit和黄瓜进行自动化测试中步骤缺失的问题。文章首先介绍了使用cucumber和Junit创建Runner类的代码,然后详细说明了黄瓜功能中的步骤和Steps类的实现。本文对于需要使用Junit和黄瓜进行自动化测试的开发者具有一定的参考价值。摘要长度:187字。 ... [详细]
  • 如何在HTML中获取鼠标的当前位置
    本文介绍了在HTML中获取鼠标当前位置的三种方法,分别是相对于屏幕的位置、相对于窗口的位置以及考虑了页面滚动因素的位置。通过这些方法可以准确获取鼠标的坐标信息。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 本文介绍了Windows Vista操作系统中的用户账户保护功能,该功能是为了增强系统的安全性而设计的。通过对Vista测试版的体验,可以看到系统在安全性方面的进步。该功能的引入,为用户的账户安全提供了更好的保障。 ... [详细]
author-avatar
用巛户khm8pcnjp9
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有