以下是如何使其工作的示例:
$(function()
{
$.each($("select.month"), function() { new MonthSelector(this); });
});
var MonthSelector = function(that)
{
var self = this;
$(that).bind("keypress", function(event) { self.onKeyPress(event); } );
this.two = false;
this.select = that;
};
MonthSelector.prototype.onKeyPress = function(event)
{
if (event.which < 48 || event.which > 57) return;
var digit = event.which - 48;
if (this.two && digit <= 2)
{
digit += 10;
this.two = false;
}
else
{
this.two = (digit == 1);
}
if (digit != 0)
this.select.selectedIndex = digit - 1;
};
为了简单起见,它很难用1-12工作. (日期和年份下拉应该根据浏览器的内置功能自动工作.)
这也不应该影响用户(例如)在12月输入“Dec”的能力.