插入日期时使用动态智能日期掩码 [英] Dynamic Smart date mask while inserting date
问题描述
在javascript或jQuery中是否有一种方法可以在键入键输入和复制并粘贴到文本框的日期时动态检测和更改?
Is there a way in javascript or jQuery to detect and change dynamically while typing a date for both a key input and a copy and paste to a text box?
我正在尝试创建一个具有功能性的文本框,其中包含两位数字,例如一个月.
I'm trying to create a functional text box which has two digits such as a month.
由于月份可以是1到12之间的数字,因此我想将第一个数字强制为1或0.
Since the month can be a number from 1 - 12 I want to enforce the first digit to be a 1 or a 0.
但是,我要尝试的技巧是当文本框首先获得焦点并且用户开始键入数字时,如果该数字是2-9,我希望零自动填充第一个位置,然后第二名是9.
The trick that I'm trying to do however is when the text box first gains focus and a user beging to type a number, if that number is 2 - 9 I want a zero to automatically fill the first spot and then but the 9 in the second spot.
在输入任何内容之前,此日期输入应如下所示:
Before I type anything this date input would look like so:
__/__/_____
如果我输入"1"
1_/__/_____
如果我输入"2"应该会得到
If I type "2" should get
02/__/_____
如果我输入"22"应该会得到
If I type "22" should get
02/2_/_____
如果我输入"77"应该会得到
If I type "77" should get
07/07/_____
我对使用代码的答案或已经执行此操作的工具的链接或到上一篇文章的链接非常感兴趣?
I would be very interested for in an answer with code or a link to a tool that already does this or a link to a previous post?
最终,我想将其放在蒙版中,这样7/7/2011或7/7/11或07/07/2011总是会填充到2011年7月7日.在最终的最终版本中,我试图将年份默认为当前十年,但每10年++--.
Eventually I want to put it in a masked so 7/7/2011 or 7/7/11 or 07/07/2011 will alway fill to 07/07/2011. In the final final version I am trying to get the year to default to the current decade but have a drop down to each 10 year++ --.
推荐答案
I believe that the functionality that you want is provided by the jQuery Masked Input plugin
演示: http://jsfiddle.net/SO_AMK/SEXAj/
请注意,它也只允许数字字符.
Please note that it also only allows numeric characters.
这篇关于插入日期时使用动态智能日期掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!