动态设置 ui datepicker max/minDate
以为这会比实际更容易。 任务:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 此网站
显然您使用了选项值来执行此操作:
i found a possible answer on this site
apparently you use the option value to do so: