简体   繁体   English

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

[英]Jquery UI DatePicker, getting associated input text for default date

I'm using Jquery Datepicker UI 1.8.20 with some server-side asp:TextBox (creation). 我正在将Jquery Datepicker UI 1.8.20与一些服务器端asp:TextBox(创建)一起使用。 To associate the Datepicker to the textboxes I add a class to them and then did the following after the body tag: 为了将Datepicker关联到文本框,我向它们添加了一个类,然后在body标记之后执行以下操作:

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

This works fine but now, I'm also populating this text-boxes server-side with a date (in a predefined format dd-MM-yyyy) and I'm trying to put the datepicker default date to be the same of its textbox but so far I haven't been able to do it. 这工作正常,但现在,我还在服务器端用日期(以预定义格式dd-MM-yyyy)填充此文本框,并且尝试将datepicker默认日期与其文本框相同但到目前为止,我还没有做到。

A few tries I made: 我做了一些尝试:

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

Error: Uncaught Invalid arguments 错误:未捕获的无效参数

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

Error: Uncaught Unknown name at position 3 错误:位置3的未知名称未知

I tried to debug the jquery in chrome to see what $(this) was and I got the window object, so I guess this is the problem. 我试图在chrome中调试jquery以查看$(this)是什么,并且得到了window对象,所以我想这就是问题所在。 I don't know how to call the associated object to the datepicker... 我不知道如何将关联的对象称为datepicker ...

UPDATE 1 - Full DatePicker code 更新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']);

Since your textbox is already populated with value, specifying just date format does the job. 由于您的文本框已经填充了值,因此仅指定日期格式即可。 Here is working jsFiddle 这是工作的jsFiddle

HTML: 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: 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"}); 
});

Try This:- 尝试这个:-

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

Working DEMO 工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM