简体   繁体   English

HTML中的自定义输入掩码

[英]Custom input mask in HTML

I want to generate an input type of text with custom mask like --day/--night and user replace - just with numbers. 我想使用自定义掩码(如--day/--night和用户替换-仅使用数字)生成文本的输入类型。 any suggestion? 有什么建议吗? Please help me out here. 请帮我在这里。

duration: --day/--night

Using two inputs is much more efficient. 使用两个输入效率更高。

But if you really want to do this, yes you can do it. 但是,如果您真的想这样做,可以的。 You would need to check the input value every time user inputs a character and format the value to what you would like. 每次用户输入字符时,您都需要检查输入值,并将值格式化为所需的格式。 You can also manipulate the cursor position using selectionStart and selectionEnd properties. 您还可以使用selectionStartselectionEnd属性来操纵光标位置。

There are also a number of mask plugins for inputs. 还有许多用于输入的遮罩插件。 http://plugins.jquery.com/tag/mask/ http://plugins.jquery.com/tag/mask/

You can do following: 您可以执行以下操作:

Make a placeholder "--" where the user can write the things in. Here is a short example what I mean: 创建一个占位符“-”,供用户在其中写入内容。这是我的简短示例:

duration: <input placeholder="--" maxlength="2">day/<input placeholder="--" maxlength="2">night

Telerik offer a free version of their "Kendo UI" JavaScript code. Telerik提供了其“ Kendo UI” JavaScript代码的免费版本。 Here's an example of what you want: http://demos.telerik.com/kendo-ui/maskedtextbox/index 这是您想要的示例: http : //demos.telerik.com/kendo-ui/maskedtextbox/index

Here's the free code: http://www.telerik.com/download/kendo-ui-core 这是免费代码: http : //www.telerik.com/download/kendo-ui-core

Here's their masked input code hosted on Github: https://github.com/telerik/kendo-ui-core/blob/master/src/kendo.maskedtextbox.js 这是托管在Github上的蒙版输入代码: https : //github.com/telerik/kendo-ui-core/blob/master/src/kendo.maskedtextbox.js

It is very hard to write this type of code but you can use this code for this type of formatting. 编写这种类型的代码非常困难,但是您可以将这种代码用于这种格式。

<div>
    <label for="phone">Phone</label>
    <!-- or set via JS -->
    <input id="phone" type="text" />
  </div>

jquery code jQuery代码

   $(":input").inputmask();

    $("#phone").inputmask({"mask": "99/99"});

see the codepen link click here 看到codepen链接点击这里

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

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