简体   繁体   中英

Cannot configure jQuery datepicker to select default date

I am using the following plugin for a datepicker:

http://xdsoft.net/jqplugins/datetimepicker/

This is a very nice plugin however I am finding it difficult to set a default date when loading the box.

See this fiddle .

$('.datepicker').datepicker();

Box 1 contains 25/12/2013. I want the datepicker to default to this date when the user selects this box. Box 2 contains nothing therefore the box should default to today.

NB This is currently using standard jquery-ui-datepicker.

I have tried setting various options with no success. I have also tried to read where the value is being set but again cannot see where the input is referenced.

Any help would be appreciated.

$('.datepicker').datepicker()
                .filter('[value!=""]')
                .datepicker('setDate', '12/25/13');

FIDDLE

As a sidenote you can set a defaultDate to any datepicker that is submitted if the user doesn't select a date, but that date is not shown as the value of the input

$('.datepicker').datepicker()
                .filter('[value!=""]')
                .datepicker( "option", "defaultDate", '12/25/13' );

In your original question you say you're using third party datepicker plugin, but in your fiddle you're using JQueryUI DatePicker. So here's an example for JQueryUI datepicker. I'm sure it can be easily updated for the third party plugin.

Try this:

HTML:

Box 1
<input type="text" class="datepicker" value="25/12/2013" />

Box 2
<input type="text" class="datepicker" value=""/>

Javascript:

$('.datepicker').each(function(index){
    var dateStr = $(this).attr("value");
    var date=new Date();

    if(dateStr.length>0)
        date=$.datepicker.parseDate("dd/mm/yy", dateStr);

    $(this).datepicker({defaultDate:date});
});

JSFiddle: http://jsfiddle.net/HVk7W/3/

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