I'm trying to add functionality to input date fields so as when the users enters in digits, slashes "/" get automatically added.(我正在尝试为输入日期字段添加功能,以便在用户输入数字时自动添加斜杠“ /”。)
So suppose I have the following html:(因此,假设我有以下html:)
<input type="text" id="fooDate" />
And suppose I have the following javascript:(并假设我有以下javascript:)
var dateField = document.getElementById("fooDate");
dateField.onkeyup = bar;
What should bar
be?(bar
应该是什么?)
So far the best google result was:(到目前为止,最好的google结果是:)
function bar(evt)
{
var v = this.value;
if (v.match(/^d{2}$/) !== null) {
this.value = v + '/';
} else if (v.match(/^d{2}/d{2}$/) !== null) {
this.value = v + '/';
}
}
Thanks!(谢谢!)
also -- I know having slashes being entered as you type sucks.(另外-我知道您输入时输入斜线很烂。) Just roll with it :p(用它滚:p)
ask by Shawn translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…