动态设置 ui datepicker max/minDate

发布于 2024-10-18 23:49:14 字数 2242 浏览 5 评论 0原文

以为这会比实际更容易。 任务:

1)禁用未来的日子。 (将 maxDate 设置为今天 - 完成)

2)从 js 变量获取日期并将其动态设置为 minDate - 如何?

main 函数(我在几页中使用):

jQuery(
    function($){
        $('#datepicker').datepicker({
             onSelect: function(dateText, inst){calendarOptions(dateText);},
            // beforeShow: function() {$("#datepicker").datepicker( "option", "minDate", curdate ).next('button').text('Kalendārs').button({icons:{primary : 'ui-icon-plus'}})}, 
             showOn: "button",
             dateFormat: 'dd.mm.yy',
             prevText: '«',
             nextText: '»',
             monthNames: ['Janvāris','Februāris','Marts','Aprīlis','Maijs','Jūnijs', 'Jūlijs','Augusts','Septembris','Oktobris','Novembris','Decembris'],
             monthNamesShort: ['JAN', 'FEB', 'MAR', 'APR', 'MAI', 'JUN', 'JUL', 'AUG', 'SEP', 'OKT', 'NOV', 'DEC'],
             dayNamesMin: ['S', 'P','O','T','C','P','S'],
             firstDay: 1,
             maxDate: calendarMaxDate()
        }).next('button').text('Kalendārs').button({icons:{primary : 'ui-icon-plus'}});
    }

);

然后是 js 代码对于我现在要编辑的页面:

function calendarMaxDate(){//disable future days
var date = new Date(); 
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
return new Date(currentYear, currentMonth, currentDate)
//return null; // set to default
}

function calendarOptions(dateText){ // sets selected date to 'external' input field
autofield_date.setValue(dateText);
}

jQuery(
    function($){ // calls function before ui datepicker button is pressed
        $('.ui-datepicker-trigger').click(function() {
        alert(autofield_dekl.getValue());
            MinDate();
        });
    }
);

function MinDate(){ //pass curdate value as'minDate' to calendar on the fly

    jQuery( //not sure about this part
        function($){ //without onload function it throws an error: $("#datepicker") is null
            $("#datepicker").datepicker( "option", "minDate", curdate ).next('button').text('Kalendārs').button({icons:{primary : 'ui-icon-plus'}})//;
        }
    )
    alert(curdate);
}

函数 MinDate() 仅在第一次按下按钮时起作用。

Thought it would be more easier than it is.
Task:

1)disable future days. (Set maxDate as today - done)

2)from js variable get date and set it as a minDate on the fly - how?

main function (which I use for few pages):

jQuery(
    function($){
        $('#datepicker').datepicker({
             onSelect: function(dateText, inst){calendarOptions(dateText);},
            // beforeShow: function() {$("#datepicker").datepicker( "option", "minDate", curdate ).next('button').text('Kalendārs').button({icons:{primary : 'ui-icon-plus'}})}, 
             showOn: "button",
             dateFormat: 'dd.mm.yy',
             prevText: '«',
             nextText: '»',
             monthNames: ['Janvāris','Februāris','Marts','Aprīlis','Maijs','Jūnijs', 'Jūlijs','Augusts','Septembris','Oktobris','Novembris','Decembris'],
             monthNamesShort: ['JAN', 'FEB', 'MAR', 'APR', 'MAI', 'JUN', 'JUL', 'AUG', 'SEP', 'OKT', 'NOV', 'DEC'],
             dayNamesMin: ['S', 'P','O','T','C','P','S'],
             firstDay: 1,
             maxDate: calendarMaxDate()
        }).next('button').text('Kalendārs').button({icons:{primary : 'ui-icon-plus'}});
    }

);

And then comes js code for page I want to edit now:

function calendarMaxDate(){//disable future days
var date = new Date(); 
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
return new Date(currentYear, currentMonth, currentDate)
//return null; // set to default
}

function calendarOptions(dateText){ // sets selected date to 'external' input field
autofield_date.setValue(dateText);
}

jQuery(
    function($){ // calls function before ui datepicker button is pressed
        $('.ui-datepicker-trigger').click(function() {
        alert(autofield_dekl.getValue());
            MinDate();
        });
    }
);

function MinDate(){ //pass curdate value as'minDate' to calendar on the fly

    jQuery( //not sure about this part
        function($){ //without onload function it throws an error: $("#datepicker") is null
            $("#datepicker").datepicker( "option", "minDate", curdate ).next('button').text('Kalendārs').button({icons:{primary : 'ui-icon-plus'}})//;
        }
    )
    alert(curdate);
}

function MinDate() works only in the first time when the button has been pressed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女 2024-10-25 23:49:14

我在 此网站

显然您使用了选项值来执行此操作:

 $("#datepicker").datepicker('option', 'minDate', newDate); 

i found a possible answer on this site

apparently you use the option value to do so:

 $("#datepicker").datepicker('option', 'minDate', newDate); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文