1

你好吗?

我有两个带有两个字段的日期选择器

我希望用户选择(From)第二个字段(TO)将是 next day 。像booking.com

例如:当用户选择From 01-01-2013 时,直接第二个字段TO 02-01-2013 (next day) ...

看这张图片的表格

在此处输入图像描述

这是jQuery的代码:

$(function() {
$( "#from" ).datepicker({
    defaultDate: "+1D",
    changeMonth: true,
    numberOfMonths: 1,
    dateFormat:"dd-mm-yy",
    minDate: "+1D",
    showOn: "button",
    buttonImage: "http://keith-wood.name/img/calendar.gif",
    buttonImageOnly: true , 

    onSelect: function(dateText, inst){
        var d = dateFormat(new Date(dateText + 1), "DD/MM/YYYY");
        $("#to").val(d);
    }

});

这是我的html代码:

<form method="get" action="">
        <input type="submit" value="choose"/>
        <p><input type="text" name="from" id="from" />From<br/>
        <input type="text" name="to" id="to" />To
        </p>
</br>

    </form>

怎么办?

4

3 回答 3

3
onSelect: function(dateText, inst){
    var d = $.datepicker.parseDate(inst.settings.dateFormat, dateText);
    d.setDate(d.getDate()+1);
    $("#to").val($.datepicker.formatDate(inst.settings.dateFormat, d));
}
于 2013-01-08T16:35:03.893 回答
2

我为您创建了一个有效的 JSFiddle:

http://jsfiddle.net/jirilmongeorge/f329k/26/

这是javascript代码:

$(document).ready(function() {
    $( "#from" ).datepicker({
    defaultDate: "+1D",
    changeMonth: true,
    numberOfMonths: 1,
    dateFormat:"dd-mm-yy",
    minDate: "+1D",
    showOn: "button",
    buttonImage: "http://keith-wood.name/img/calendar.gif",
    buttonImageOnly: true , 
    onSelect: function(dateText, inst){
        var d = $.datepicker.parseDate(inst.settings.dateFormat, dateText);
        d.setDate(d.getDate()+1);
        $("#to").val($.datepicker.formatDate(inst.settings.dateFormat, d));
    }

   });

   $( "#to" ).datepicker({
        showOn: "button",
        buttonImage: "http://keith-wood.name/img/calendar.gif",
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true
    });

  $('#from').attr('readonly', true);
  $('#to').attr('readonly', true);
});
于 2013-01-08T16:59:21.243 回答
1

您需要将 dateText 转换为正确的日期格式,然后加一并设置回 #to 值

var from = dateText.split("-");
var fDate = new Date(from[2], from[1] - 1, from[0]);

var newdate = new Date(fDate);
newdate.setDate(newdate.getDate() + 1);
$("#to").val(newdate.getDate() + "-" + newdate.getMonth() + "-" + newdate.getFullYear());
于 2013-01-08T16:34:53.640 回答