Bootstrap 中的开始日期和结束日期 [英] Start Date and End Date in Bootstrap

查看:24
本文介绍了Bootstrap 中的开始日期和结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Bootstrap DatePicker.我想验证 From DateTo Date .开始日期正确选择今天的日期.我有一个问题迄今为止没有选择开始日期(即从日期值)".如何解决?

I am using Bootstrap DatePicker. I want to Validate From Date and To Date . Start Date correctly Pick todays date.I have a Problem " To Date does not Pick the start date(ie. from date value)" .How to solve it?

  $(document).ready(function() {
    $('#fromDate').datepicker({
        startDate: new Date(),
    });

    $('#toDate').datepicker({
        startDate: $('#fromDate').val(),
    });
 });

推荐答案

在设置 $('#toDate').datepicker() 时,$('#fromDate') 为空.

At the time you set $('#toDate').datepicker() , the value of $('#fromDate') is empty.

您应该在开头设置默认的 startDateendDate 变量并添加 changeDate 事件侦听器到您的日期选择器.

You should set default startDate and endDate variables at the begining and add changeDate event listener to your datepickers.

例如:

// set default dates
var start = new Date();
// set end date to max one year period:
var end = new Date(new Date().setYear(start.getFullYear()+1));

$('#fromDate').datepicker({
    startDate : start,
    endDate   : end
// update "toDate" defaults whenever "fromDate" changes
}).on('changeDate', function(){
    // set the "toDate" start to not be later than "fromDate" ends:
    $('#toDate').datepicker('setStartDate', new Date($(this).val()));
}); 

$('#toDate').datepicker({
    startDate : start,
    endDate   : end
// update "fromDate" defaults whenever "toDate" changes
}).on('changeDate', function(){
    // set the "fromDate" end to not be later than "toDate" starts:
    $('#fromDate').datepicker('setEndDate', new Date($(this).val()));
});

这篇关于Bootstrap 中的开始日期和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆