jquery: using two datepicker with two fields ( field 1 , field2 + 1 day ) like booking.com

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 10:52:37

问题


How r u?

I have two datepickers with two fields

I want when the user choosing (From) the second field ( TO ) will be next day . like booking.com

for example: when the user choose From 01-01-2013 , directly the second field TO 02-01-2013 (next day) ...

look at this picture for the form

this is code of 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);
    }

});

this is my html code:

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

how can do it?


回答1:


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));
}



回答2:


I have created a working JSFiddle for you:

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

Here is the javascript code:

$(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);
});



回答3:


you need to convert your dateText into the right format of date, then plus one and set back to the #to value

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());


来源:https://stackoverflow.com/questions/14219296/jquery-using-two-datepicker-with-two-fields-field-1-field2-1-day-like-b

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