我使用flatpickr.js库创建了一个日历。我只想启用从下拉列表(属性名称为“ destinazione”)中选择的arreys dateCorfu,dateZante和datePag中存在的日期。我不是编码人员,但是我使用eval方法编写了以下代码,此方法可以正常工作,但我确定这是错误的方法。.也许有更好的方法来处理function(date)的结果。那么有没有办法使它在不使用eval的情况下起作用?非常感谢
flatpickr("#data-partenza", {
locale:'it',
minDate: "2020-07-16",
enable: [
function(date) {
// return true to enable
var drop_destinazione = jQuery('[name="destinazione"]').val();
var result = '';
var dateCorfu = ["2020-07-16","2020-07-23","2020-07-30","2020-08-06","2020-08-13","2020-08-20"];
var dateZante = ["2020-07-17","2020-07-24","2020-07-31","2020-08-07","2020-08-14"];
var datePag = ["2020-07-18","2020-07-25","2020-08-01","2020-08-08"];
var slice = 'date.toISOString().slice(0,10)';
var i;
if(drop_destinazione == 'Corfù'){
for (i = 0; i < dateCorfu.length; i++) {
result += (slice + '==' + '"' + dateCorfu[i] + '"' + '||');
}
return eval(result.slice(0, -2));
}
else if(drop_destinazione == 'Zante'){
for (i = 0; i < dateZante.length; i++) {
result += (slice + '==' + '"' + dateZante[i] + '"' + '||');
}
return eval(result.slice(0, -2));
}
else if(drop_destinazione == 'Pag'){
for (i = 0; i < datePag.length; i++) {
result += (slice + '==' + '"' + datePag[i] + '"' + '||');
}
return eval(result.slice(0, -2));
}
else{console.log('no destination selected')}
}
],
dateFormat: "d-m-Y",
disableMobile: true,
});
由于您仅在进行if验证,所以为什么不立即对每个结果+ = ..进行评估和累积?
result = true;
for (...) {
if (condition) result = false;
}
return result;