jQuery-UI日期選擇器中的最小日期設置(minDate)應設置爲生日(在我的代碼中爲birthdateDMY)。我正在考慮在.datepicker({})中使用匿名函數,但我不知道如何使其工作。將minDate設置爲jQuery數據添加日期中的動態日期
//birthdateDMY will be something like [ "01", "07", "2015" ]
var name = jQuery("#dropdown").val();
var index = baby.Name.indexOf(name);
var birthdateDMY = baby.BirthDate[index].split("/");
jQuery(function() {
jQuery("#datepicker").datepicker({
//need an anonymous function here to set minDate?
minDate: (new Date(birthdateDMY[2], birthdateDMY[1] - 1,
birthdateDMY[0])),
maxDate: 0,
dateFormat: "dd/mm/yy"
});
解決使用二傳手:
//Setter: update minDate for e.g. #datepicker
function updateMinDate(selector){
//birthdateDMY will be something like [ "01", "07", "2015" ]
var name = jQuery("#dropdown").val();
var index = baby.Name.indexOf(name);
var birthdateDMY = baby.BirthDate[index].split("/");
//Update minDate in the selected item
$(selector).datepicker("option", "minDate", new Date(birthdateDMY[2], birthdateDMY[1] - 1, birthdateDMY[0]));
}
//When changin #dropdown, call updateMinDate
jQuery(document).on('change', '#dropdown', function(e) {
updateMinDate("#datepicker");
}
jQuery(function() {
jQuery("#datepicker").datepicker({
minDate: <some code run when page loads>,
maxDate: 0, numberOfMonths: 2, dateFormat: "dd/mm/yy"
});
});
也許你可以設置一個驗證函數到日期選擇器字段,所以它不會允許用戶提交表單,而de date不符合標準。 – Aleuck