【问题标题】:Bootstrap Datepicker,checkout input > checkin input引导日期选择器,结帐输入>签入输入
【发布时间】:2018-02-07 17:22:50
【问题描述】:

我的表单中有两个字段。一个用于入住日期,另一个用于退房日期。我正在使用 [bootstrap datepicker][1] 进行日期选择。我想要实现的是,一旦设置了签入值,minimal(startDate) 签出值将是签入值 + 1 天。这是我当前的代码:

HTML:

        <form id="homeBookForm">
        <div class="inner-addon right-addon">
            <i class="fa fa-calendar" aria-hidden="true" id="checkInIcon"></i>
            <input type="text" class="form-control checkIn" 
            placeholder="Check-In">
        </div>
        <div class="inner-addon right-addon">
            <i class="fa fa-calendar" aria-hidden="true" id="checkOutIcon"></i>
            <input type="text" class="form-control checkOut" placeholder="Check-Out">
        </div>
        <button>Book Now</button>
    </form>

JS:

var date = new Date();
$('.checkIn').datepicker({
    format:'yyyy-mm-dd',
    todayBtn:true,
    autoclose:true,
    startDate:date
}).on('blur',function(){
    $('.checkOut').focus();
});
$('.checkOut').datepicker({
    format:'yyyy-mm-dd',
    todayBtn:true,
    autoclose:true,
    startDate:$('.checkIn').val()
});

现在检查字段的代码工作正常,但是在自动关闭发生后,结帐字段只是集中并且日期选择器甚至没有弹出。有人知道我做错了什么吗?提前致谢。 :) [1]:http://bootstrap-datepicker.readthedocs.io/en/latest/index.html

【问题讨论】:

    标签: javascript html jquery datepicker


    【解决方案1】:

    您可以使用日期选择器的changeDate 事件来实现您想要的一切。 尝试将您当前的实现更改为此

    var date = new Date();
    $(".checkIn").datepicker({
        format: "yyyy-mm-dd",
        todayBtn: true,
        autoclose: true,
        startDate: date
      })
      .on("changeDate", function(e) {
        var checkInDate = e.date, $checkOut = $(".checkOut");
        checkInDate.setDate(checkInDate.getDate() + 1);
        $checkOut.datepicker("setStartDate", checkInDate);
        $checkOut.datepicker("setDate", checkInDate).focus();
      });
    
    $(".checkOut").datepicker({
      format: "yyyy-mm-dd",
      todayBtn: true,
      autoclose: true
    });
    

    您不再需要 blur 事件,因为日期选择器的 changeDate 事件将负责将焦点设置到您的结帐日期选择器。

    在这里查看演示 https://codepen.io/anon/pen/ZrByVp?editors=1010

    【讨论】:

    • 嘿,非常感谢,我遇到的唯一问题是用户仍然可以选择入住日期作为退房日期,入住日期不会被阻止。我应该更改什么以确保无法在退房字段中选择入住日期。
    • @KristianM 我已经更新了我的答案。更改是在入住日期中添加一天后设置退房的开始日期。
    猜你喜欢
    • 1970-01-01
    • 2016-09-18
    • 2018-05-09
    • 2016-12-27
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多