jQuery datepicker - defaultDate (dynamically) from another datepicker

不想你离开。 提交于 2021-02-10 07:14:35

问题


I have two datepickers at the moment, one for a start date, and another for an end date.

When you select the first date, I'd like the default value of the second datepicker to be set to the same date. If I use the setDate it works and sets the value just fine, but the default value won't change - if I set the defaultDate when the datepicker is loaded, it works, but if I try to set it on the fly it doesn't.

Here's my code:

if($('#startDate').val().length == 10 && $("#endDate").val().length != 10) {
    $("#endDate").datepicker("defaultDate", $("#startDate").datepicker("getDate"));
}

(edit) for clarification, I don't want to set the date - the user needs to do that, but I'd like the datepicker to go straight to the same date as the start date, to save them having to trawl through numerous pages.

Here's the (kinda) working code: http://jsfiddle.net/c8H5y/

Any suggestions would be very welcome, thank you!


回答1:


This should do it:

$("#dep_date").datepicker({
    dateFormat: 'dd/mm/yy',
    onSelect: function(dateText, inst) {
        var date = $.datepicker.parseDate('dd/mm/yy', dateText);
        var $ret_date = $("#ret_date");
        $ret_date.datepicker("option", "defaultDate", date);
    }
});

$("#ret_date").datepicker({
    dateFormat: 'dd/mm/yy',
});

jsFiddle



来源:https://stackoverflow.com/questions/7598710/jquery-datepicker-defaultdate-dynamically-from-another-datepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!