简体   繁体   English

HTML 输入:默认强制使用数字键盘,但允许使用字母

[英]HTML Input: force numeric keyboard by default but allow letters

I have an HTML input.我有一个 HTML 输入。 The input is on the web page which is opened in Chrome on an Android phone.输入是在 Android 手机上的 Chrome 中打开的网页上。

I want an option to let the user to see a numeric keyboard when he starts entering the value.我想要一个选项让用户在开始输入值时看到数字键盘。 But at the same moment, I want him to have a possibility to enter alphanumeric characters.但同时,我希望他能够输入字母数字字符。

  • I cannot use type="number" because it doesn't allow entering letters.我不能使用type="number"因为它不允许输入字母。

  • I cannot use type="text" because it opens alpha keyboard by default and a user have to switch to numeric keyboard.我不能使用type="text"因为它默认打开字母键盘,用户必须切换到数字键盘。

So the option I'm trying to find is when the standard alpha-numeric keyboard got opened but the digits input is already selected (Like when you press ?123 on the standard keyboard).所以我试图找到的选项是当标准字母数字键盘打开但数字输入已经被选中时(就像你在标准键盘上按 ?123 时一样)。

I have tried to use type="tel" but I don't understand how to switch to letters from numbers.我曾尝试使用 type="tel" 但我不明白如何从数字切换到字母。

I'm using Cordova, so if there's no option to do this using HTML I could use native plugins if you suggest me any of them.我正在使用 Cordova,所以如果没有使用 HTML 的选项,我可以使用本机插件,如果你建议我使用它们中的任何一个。


I use cordova so if there's no HTML way to do things I'm ready to integrate any sort of plugin, if you could suggest me anyone.我使用cordova ,所以如果没有HTML 方式来做事,我准备好集成任何类型的插件,如果你能建议我的话。


TL;DR: This is what I want to see as a default keyboard layout. TL;DR:这是我希望看到的默认键盘布局。 Is it possible?可能吗?

在此处输入图像描述

You probably want to take a look at this: https://css-tricks.com/finger-friendly-numerical-inputs-with-inputmode/你可能想看看这个: https ://css-tricks.com/finger-friendly-numerical-inputs-with-inputmode/

<input inputmode="numeric" pattern="[0-9]*" type="text" name="creditcard">

The inputmode will work on the latest Android browsers, and the pattern is a hack for iOS.输入inputmode将适用于最新的 Android 浏览器,并且该pattern是 iOS 的 hack。 Pay special attention to the article where they talk about iOS not allowing the user to change keyboards.请特别注意他们谈论 iOS 不允许用户更改键盘的文章。

Hope it helps.希望能帮助到你。

Full solution to your problem using JavaScript.使用 JavaScript 全面解决您的问题。

The trick is to create a second number input, which you overlap on top of your text input using css.诀窍是创建第二个数字输入,您使用 css 将其重叠在文本输入之上。

<style type="text/css">
    /* hide number spinners on inputs */
    input::-webkit-outer-spin-button,
    input::-webkit-inner-spin-button {
        -webkit-appearance: none;
        margin: 0;
    }
    input[type=number] {
        -moz-appearance:textfield; /* Firefox */
    }
    .hidden-number {
        margin-top: -26px;
    }
</style>


<form method="post" action="submit.php">
    <input class="form-control" type="text" name="input_name">
    <input class="form-control hidden-number" type="number" id="input_name">
</form>

Using JavaScript, when your number input gains focus it will trigger the keyboard that you want.使用 JavaScript,当您的数字输入获得焦点时,它将触发您想要的键盘。 You will then have to remove the type='number' attribute, which would prevent you from entering anything other than numbers.然后,您必须删除 type='number' 属性,这将阻止您输入除数字以外的任何内容。 Then transfer whatever content is in your text input to the number input.然后将文本输入中的任何内容传输到数字输入。 Lastly, when the number input loses focus, transfer its contents back to the text input and replace its type='number' attribute.最后,当数字输入失去焦点时,将其内容传输回文本输入并替换其 type='number' 属性。

<script type="text/javascript">
    $(".hidden-number").on("focus", function(){
        $(this).removeAttr("type");
        var text_input = $("input[name="+this.id+"]");
        $(this).val(text_input.val());
        text_input.val("");
    });

    $(".hidden-number").on("focusout", function(){
        var text_input = $("input[name="+this.id+"]");
        text_input.val($(this).val());
        $(this).attr("type", "number");
    });
</script>

You cannot do this as the keyboard layout provided to the user is based on the input type of your input.您不能这样做,因为提供给用户的键盘布局基于您输入的输入类型。 It would be a bad UI principle to show the user a numeric keypad, when he can in reality enter alphanumeric characters.当用户实际上可以输入字母数字字符时,向用户显示数字键盘将是一个糟糕的 UI 原则。 The tel input type won't let you change the keyboard to a numeric one, since it is specifically designed to accept only phone numbers. tel 输入类型不允许您将键盘更改为数字键盘,因为它专门设计用于仅接受电话号码。

So if you want to let the user enter alphanumeric characters in your input, you should not set the keyboard to be numeric.因此,如果您想让用户在输入中输入字母数字字符,则不应将键盘设置为数字。 See the example below:请参见下面的示例:

<input type="text" name="valText"/>

If on the other hand you want to allow the user to only enter numeric characters, then you should set the input type to number as below:另一方面,如果您希望用户只输入数字字符,那么您应该将输入类型设置为数字,如下所示:

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

A workaround to this would be to create multiple inputs and set their type to whatever you want the user to enter in that particular field.解决此问题的方法是创建多个输入并将其类型设置为您希望用户在该特定字段中输入的任何内容。 You can then concatenate them to a single value to save them in that way.然后,您可以将它们连接到单个值以以这种方式保存它们。 However you will need to save them as a concatenated String which contains both characters and numbers.但是,您需要将它们保存为包含字符和数字的串联字符串。

You can also take a look at the link here to check the different input types you can use in case you find one of them more of use to you.您还可以查看此处的链接以检查您可以使用的不同输入类型,以防您发现其中一种对您更有用。 What you must remember however is that the input type will define the keypad layout.但是,您必须记住的是输入类型将定义键盘布局。 You may also take a look at this for a better understanding.你也可以看看这个以获得更好的理解。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM