简体   繁体   English

jquery 2 个日历一个设置范围另一个

[英]jquery 2 calendars one setting range for the other

I am using two jquery on one page.我在一页上使用了两个 jquery。 On Document ready one of them is set to the current date, the other is set to the current date + 15 days在 Document ready 中,其中一个设置为当前日期,另一个设置为当前日期 + 15 天

What I am trying to do is make the second calendar never allow to choose a date that is earlier to the first +15 days.我想做的是让第二个日历永远不允许选择早于前 +15 天的日期。

Example: on Document ready calendar 1: shows 07/05/11 calendar 2: shows 07/20/11,If i choose 7/21/11 on calendar then calendar 2 should display: 07/05/11.示例:在文档就绪日历 1:显示 07/05/11 日历 2:显示 07/20/11,如果我在日历上选择 7/21/11,则日历 2 应显示:07/05/11。 only on the focus of the second datepicker input field will the value of the field change to the 5th and display correctly in calendar view.只有在第二个日期选择器输入字段的焦点上,该字段的值才会更改为第 5 个并在日历视图中正确显示。 It should do this automatically.它应该自动执行此操作。

Here is an example of how this is being handled这是如何处理的示例

// Setting the current date and adding 15 days 

function setRange() {
    d=new Date($('#dateofchange').datepicker('getDate'));
    d.setDate(d.getDate() + 15);
    $('#EffectiveDate').datepicker('option', 'minDate', new Date(d));
    d=new Date($('#dateofchange').datepicker('getDate'));
    d.setFullYear(d.getFullYear() + 1);
    $('#EffectiveDate').datepicker('option', 'maxDate', new Date(d));
}

// This is the initial datePicker

$('#dateofchange').datepicker({
    showButtonPanel: true,
    minDate: new Date()
    });

// This is the current Date plus 15 days. 

$('#EffectiveDate').datepicker({
    showButtonPanel: true,
    beforeShow: setRange
});

I would rewrite it a tad and do it something like this.我会稍微改写一下,然后做这样的事情。

When a date gets selected in the #dateofchange datepicker, the minDate and maxDate options of the #EffectiveDate will get set again, based on the date the user has selected.#dateofchange日期选择器中选择日期时,将根据用户选择的日期再次设置#EffectiveDateminDatemaxDate选项。

function addDays(date) {
    var d = new Date(date);
    d.setDate(d.getDate() + 15);   
    return d;
}
function addYear(date){
    var d = new Date(date);
    d.setFullYear(d.getFullYear() + 1);
    return d;
}

$('#dateofchange').datepicker({
    showButtonPanel: true,
    minDate: new Date(),

    // change EffectiveDate minDate and maxDate when the user selects a date in #dateofchange
    onSelect:function(dateText, inst) {
        var d = new Date(dateText);
        $("#EffectiveDate").datepicker( "option", "minDate", addDays(d));
        $("#EffectiveDate").datepicker( "option", "maxDate", addYear(d));        
    }
});

$('#EffectiveDate').datepicker({
    showButtonPanel: true,
    minDate: addDays(new Date()),
    maxDate: addYear(new Date())
});

It could probably be written a bit more concise and elegant, but this should function the way you described.它可能会写得更简洁和优雅,但这应该 function 你描述的方式。

jsFiddle: http://jsfiddle.net/bjorn/aVBca/2/ jsFiddle: http://jsfiddle.net/bjorn/aVBca/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM