Jquery UI DatePicker,获取默认日期的关联输入文本

时间:2014-12-10 08:54:29

标签: javascript jquery asp.net jquery-ui datepicker

我使用Jquery Datepicker UI 1.8.20和一些服务器端asp:TextBox(创建)。 要将Datepicker与文本框关联,我向它们添加一个类,然后在body标记后面执行以下操作:

 $('body').on('focus', ".datepicker_input", function() {
 ...
 });

这很好但现在,我还在这个文本框服务器端填充日期(以预定义格式dd-MM-yyyy)并且我试图将datepicker默认日期设置为和它的文本框一样,但到目前为止我还没能做到。

我做了几次尝试:

 $('body').on('focus', ".datepicker_input", function() {
    $(this).datepicker({
        defaultDate: $.datepicker.parseDate("dd-MM-yy", $(this).value),
 ...
 });

错误:未捕获无效参数

 $('body').on('focus', ".datepicker_input", function() {
    $(this).datepicker({
        defaultDate: $.datepicker.parseDate("dd-MM-yy", this.value),
 ...
 });

错误:位置3处未捕获的未知名称

我尝试在chrome中调试jquery以查看$(this)是什么,我得到了window对象,所以我想这就是问题所在。我不知道如何将相关对象调用到datepicker ...

更新1 - 完整的DatePicker代码

$('body').on('focus', ".datepicker_input", function() {
    $(this).datepicker({
        defaultDate: $.datepicker.parseDate("dd-MM-yy", $(this).value),
        minDate: 0,
        beforeShowDay: $.datepicker.noWeekends,
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1
    });
$.datepicker.regional['pt-PT'] = {
        closeText: 'Fechar',
        prevText: '',
        nextText: '',
        currentText: 'Hoje',
        monthNames: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro',
                        'Outubro', 'Novembro', 'Dezembro'],
        monthNamesShort: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro',
                        'Outubro', 'Novembro', 'Dezembro'],
        dayNames: ['Domingo', 'Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sabado'],
        dayNamesShort: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab'],
        dayNamesMin: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab'],
        weekHeader: 'Sm',
        dateFormat: 'dd-mm-yy'
    };
    $.datepicker.setDefaults($.datepicker.regional['pt-PT']);

2 个答案:

答案 0 :(得分:2)

由于您的文本框已填充了值,因此仅指定日期格式即可完成作业。这是工作jsFiddle

HTML:

<input id="datepicker" class="datepicker_input" type="text" VALUE="05-april-2014"> 
<input id="datepicker" class="datepicker_input1" type="text" VALUE="05-11-2014">         

JS:

$('body').on('focus', '.datepicker_input', function() { 
      $(this).datepicker({dateFormat:"dd-MM-yy"}); 
});
$('body').on('focus', '.datepicker_input1', function() { 
      $(this).datepicker({dateFormat:"dd-mm-yy"}); 
});

答案 1 :(得分:0)

试试这个: -

$('body').on('focus', '.datepicker_input', function() {
  $(this).datepicker({
    dateFormat: 'dd-MM-yy',
    defaultDate: $(this).val(),
    ...
  });
});

Working DEMO