jQuery触发DatePicker更改事件 [英] jQuery trigger a DatePicker change event

查看:782
本文介绍了jQuery触发DatePicker更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

$('.custom_datepicker_selector').datepicker({
  weekStart: 1
})
.on('changeDate', function(en) {
  var correct_format;
  correct_format = en.date.getFullYear() + '-' + ('0' + (en.date.getMonth() + 1)).slice(-2) + '-' + ('0' + en.date.getDate()).slice(-2);
  $('.custom_datepicker_selector').datepicker('hide');
  return $(this).parent().find("input[type=hidden]").val(correct_format);
});

这将显示日期格式,就像我想要的那样.但是,只有在我单击日期选择器之后才这样做,而不是最初.

This displays the date format just like I want it to. However it only does so after I click on the datepicker, not initially.

最初显示此日期:

2013-02-17

在我点击它后,我得到了:

and after I click on it I get this:

17/02/2013

如何立即显示正确的日期? (以上代码位于.ready

How can I display the correct date immediately? (the code above is in the .ready

我为此创建了一个jsFiddle: http://jsfiddle.net/jwxvz/

I created a jsFiddle for this: http://jsfiddle.net/jwxvz/

这比JavaScript更像是一个Rails问题:

This was more a rails problem than a javascript:

我遵循了abu的建议,并且做到了:

I followed abu advice and did it like this in rails:

<%= f.input :order_date, :as => :custom_datepicker, :input_html =>  { :value => localize(@client_order.order_date) } %>

推荐答案

您还可以定义默认日期格式.

You can define the default date format also.

尝试一下:

$('.custom_datepicker_selector').datepicker({
  weekStart: 1,
  dateFormat: 'dd/mm/yy'
}).on('changeDate', function(en) {
      $('.custom_datepicker_selector').datepicker('hide');
      return $(this).parent().find("input[type=hidden]").val(en);
   });

更新:(重要)

我看过您的JSFiddle,您将文本框的默认值保留为value="2013-02-17",这就是为什么它在开始时只显示该值的原因.

I have seen your JSFiddle, you have kept the default value of the textbox as value="2013-02-17" that's why it shows that value at start just remove it.

这篇关于jQuery触发DatePicker更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆