2010-10-28 67 views
1

我有一个使用jqueryui DatePicker的Datepicker。jQueryUI DatePicker自定义年

行为的要求是:

  • 用户可以存储生日的朋友。如果已知,用户应该能够存储一年,或选择“不适用”。

  • 如果选择“不适用”,请将1900年存储在数据库中,并且只在用户的输入字段中显示日期和月份。

  • 下拉应该开始于1950年,但显示从当前日期在过去的100年(< - 这将上锦上添花,否则在启动当年,如果需要回到1900年)

这是我到目前为止,但它kludgey ...这是一个轻描淡写。由于某种原因,如果用户选择日期,但不改变年份下拉,那么当前年份将被存储,而不是1900年。

(它在json调用的上下文中)。当然有更好的方法。

var birthday_options = {changeMonth: true, changeYear: true, yearRange: '1900:2010', 
     onClose: function(dateText, inst) { 
     contact.field_updater('birthday').call(this); 
     $('input.mng-birthday').val(function(i, val) { 
         return val.replace('/1900', ''); 
        }); 
     }, 
     beforeShow: function (input) { 
         setTimeout(function() { 
         $('select.ui-datepicker-year option:first').text('N/A');                 
         }, 1); 
        }}, 

.find('.mng-birthday').val(data.birthday || 'Unknown') 
    .datepicker(birthday_options) 
.end(); 

回答

0

Click here to see a demo.

当你发现自己超出范围时,jQuery UI的来源是很容易修改。你只需要改变函数定义为_generateMonthYearHeader:

// StackOverflow question: Add option for N/A 
html += '<option value="1900">N/A</option>'; 

,使这个改变_selectDay到ommit年当选择N/A。

// StackOverflow question: format the date 
if (inst.currentYear == 1900) 
    this._selectDate(id, (inst.currentMonth + 1) + "/" + inst.currentDay); 
else 
    this._selectDate(id, this._formatDate(inst, inst.currentDay, 
        inst.currentMonth, inst.currentYear)); 

在你的核心用户界面脚本,修改后的datpicker脚本,安装脚本面板:

$(document).ready(function() { 
    var year = new Date().getFullYear(); 

    $(".mng-birthday").datepicker({ 
     changeYear: true, 
     // The year range is from the current year to 100 years before 
     yearRange: (year - 100) + ":" + year, 
     // The default date is january 1st fifty years before the current year 
     defaultDate: "01/01/1900" 
    }); 
}); 
+0

我看这应该是如何工作的,显然不会对演示,但是我继续去选择2016年,当我选择N/A。 – kinet 2010-10-28 23:07:41