简体   繁体   中英

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

<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. 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.

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 :

altFormat
The dateFormat to be used for the altField option.

and:

altField An input element that is to be updated with the selected date from the datepicker. Use the altFormat option to change the format of the date within this field.

The corresponding examples only show each option being used alone but you need to specify altField if you want to use 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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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