Javascript jQuery 触发 DatePicker 更改事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14919563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:19:38  来源:igfitidea点击:

jQuery trigger a DatePicker change event

javascriptruby-on-rails-3jquery-ui

提问by rept

I have the following code:

我有以下代码:

$('.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.

这就像我想要的那样显示日期格式。然而,它只在我点击日期选择器后才这样做,而不是最初。

Initially this date is shown:

最初显示此日期:

2013-02-17

and after I click on it I get this:

在我点击它之后,我得到了这个:

17/02/2013

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

如何立即显示正确的日期?(上面的代码在 .ready

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

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

This was more a rails problem than a javascript:

这更像是一个 rails 问题而不是 javascript:

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

我遵循了 abu 的建议,并在 Rails 中这样做了:

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

回答by Abubakkar

You can define the default date format also.

您也可以定义默认日期格式。

Try this out:

试试这个:

$('.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);
   });

UPDATE : (important)

更新:(重要)

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.

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

回答by sourcecode

just you have to use:

只是你必须使用:

$('.custom_datepicker_selector').datepicker({
weekStart: 1,
dateFormat:'yy-mm-dd'
})

you can reference here : link

你可以参考这里:链接