【问题标题】:How do I link two input fields of type date?如何链接两个日期类型的输入字段?
【发布时间】:2017-03-20 23:41:35
【问题描述】:

我有两个输入字段:
<input name="check_in" type="date" class="checkin">
<input name="check_out" type="date" class="checkout"
我想链接它们,所以当我从check_in 字段中选择一个日期时,我只能选择当天或以后的一天(之前的日子被禁用)。之后,我必须在check_out 字段中选择一天,我只能选择在check_in 中选择的日期或以后的一天(其他日期必须禁用)

我一直在尝试几种解决方案和代码 sn-ps 但没有结果

【问题讨论】:

    标签: forms laravel date input


    【解决方案1】:

    如果您使用的是 Bootstrap 日期选择器,那么您可以将check_out 的开始日期分配给check_in 上的日期。类似的东西

    $('.checkin').datepicker({
        startDate: 'd' //this means today
    });
    
    $('.checkout').datepicker({
        startDate: $('.checkin').val(); //Not sure of this but you get the idea
    });
    

    替代方案,只需使用 daterage 选项,它已经实现了所有功能:

    <div class="input-group input-daterange">
        <input type="text" class="form-control" value="2012-04-05">
        <div class="input-group-addon">to</div>
        <input type="text" class="form-control" value="2012-04-19">
    </div>
    

    以及与

    配套的 Javascript
    $('.input-daterange input').each(function() {
        $(this).datepicker('clearDates');
    });
    

    更多:https://bootstrap-datepicker.readthedocs.io/en/latest/markup.html#date-range

    这将为您提供开箱即用的所需。

    【讨论】:

    • 问题中没有助推器。
    猜你喜欢
    • 2020-09-07
    • 1970-01-01
    • 2016-11-10
    • 2015-02-21
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 2019-08-23
    相关资源
    最近更新 更多