Luk*_*uha 6 javascript jquery nette
我正在尝试在jquery中为几个日期选择器设置不同的选项.我的代码看起来像这样:
{foreach $cart->getItems() as $item}
{if $item->action->prereservation}
var disableDates = new Array();
{if $item->action->hasVariants()}
disableDates[{!$item->id}] = {$disabledDates[$item->action->id][$item->idVariant]};
{else}
disableDates[{!$item->id}] = {$disabledDates[$item->action->id]};
{/if}
if (disableDates[{!$item->id}].length !== 0) {
$(".datepicker_"+'{$item->id}').datepicker({
maxDate: new Date('{!$item->action->voucherTo|date: Y-m-d}'),
beforeShowDay: function(date){
var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
console.log(disableDates[{!$item->id}]) // result is undefined (but not for last iteration)
return [ disableDates[{!$item->id}].indexOf(string) == -1 ]
}
})
} else {
$(".datepicker_"+'{$item->id}').datepicker({
maxDate: new Date('{!$item->action->voucherTo|date: Y-m-d}'),
})
}
{/if}
{/foreach}
Run Code Online (Sandbox Code Playgroud)
但如果foreach中有多个项目,我的js控制台显示错误无法读取第一次迭代的未定义属性'indexOf',只有最后一次是好的.有人能帮帮我吗?
在我的代码我结合模板系统Latte和jquery.
这是我在浏览器中的最终代码:
var disableDates = new Array();
disableDates[777955] = ["2014-07-25","2014-07-26","2014-07-27","2014-07-28","2014-07-29","2014-07-30","2014-07-31"];
if (disableDates[777955].length !== 0) {
$(".datepicker_"+'777955').datepicker({
maxDate: new Date('2014-07-31'),
beforeShowDay: function(date){
var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
return [ disableDates[777955].indexOf(string) == -1 ]
}
})
} else {
$(".datepicker_"+'777955').datepicker({
maxDate: new Date('2014-07-31'),
})
}
Run Code Online (Sandbox Code Playgroud)
感谢您的任何建议
如果您在循环中执行此操作,则会继续覆盖数组!
var disableDates = new Array();
disableDates[123] = 123;
console.log(disableDates[123]); //123
var disableDates = new Array();
disableDates[456] = 456;
console.log(disableDates[123]); //undefined
Run Code Online (Sandbox Code Playgroud)
将数组声明移出循环或在创建新数组之前检查它是否存在。