简体   繁体   English

Rails应用程序中的jQuery altFormat datepicker问题

[英]Jquery altFormat datepicker issue in rails app

I have a form with a date input 我有一个带有日期输入的表格

<div class="form-inputs">
<%= f.input :day, as: :string, input_html: {id: 'date'} %>

And i'm using a datepicker jquery object that I make at the bottom of the page 我正在使用在页面底部创建的datepicker jQuery对象

<script>$(function(){$("#date").datepicker();});</script>

When I have this, everything works, and the datepicker displays as it should, but it displays as month date year, whereas the db and rails takes date month year. 当我有了这个时,一切正常,并且datepicker会按其应有的方式显示,但是它显示为month date year,而db和rails则使用date month year。 So some work when they should not ie (05/07/2013) and then obviously when the date is over 12, it rejects it saying that the field is blank. 因此,有些工作在他们不应该这样做的时候(即2013年5月7日),然后显然当日期超过12时,它拒绝说该字段为空。

I have tried 我努力了

<script>$(function(){$("#date").datepicker({ dateFormat: "dd-mm-yy",altFormat: "ddmmyy"});</script>

<script>$(function(){$("#date").datepicker({ altFormat: "dd-mm-yy" });</script>

And none of them have worked, am I missing something or do I need to change something somewhere else? 他们都没有工作,我是否缺少某些东西,还是需要在其他地方进行更改?

The examples in the docs are little incomplete. 文档中的示例并不完整。 The altFormat option is only used if you also specify the altField : 仅当您还指定altField才使用altFormat选项:

altFormat altFormat
The dateFormat to be used for the altField option. 用于altField选项的dateFormat

and: 和:

altField An input element that is to be updated with the selected date from the datepicker. altField将使用日期选择器中的选定日期更新的输入元素。 Use the altFormat option to change the format of the date within this field. 使用altFormat选项可以更改此字段中日期的格式。

The corresponding examples only show each option being used alone but you need to specify altField if you want to use altFormat . 相应的例子只能说明每个选项单独使用,但你需要指定altField如果你想使用altFormat The usual approach would be to use a hidden input as the actual date that gets sent to the server and then hook up the user-visible date selector to the hidden input using altField with something like this: 通常的方法是使用隐藏的输入作为发送到服务器的实际日期,然后使用altField将用户可见的日期选择器连接到隐藏的输入,如下所示:

$('#date').datepicker({
    dateFormat: 'dd-mm-yy',
    altField: '#some-hidden-field',
    altFormat: 'yy-mm-dd',
});

Demo: http://jsfiddle.net/ambiguous/jWT5G/ 演示: http//jsfiddle.net/ambiguous/jWT5G/

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

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