【发布时间】:2019-08-14 17:00:29
【问题描述】:
在下面的代码中,我有一个函数将搜索包含日期、公园和相应的公园时间的二维数组。
它应该返回到控制台什么是在 bootstrap-datepicker 中选择的日期的公园时间,以及位于以下两行中的以下两个条目二维数组(您会注意到数据将以 3 个为一组,因为要报告三个公园的小时数)。
但是,控制台什么也没显示。我是不是走错了路,还是我的代码有错误?
<head>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker3.css">
</head>
<body>
<div id="calendar-container"></div> <!-- 9/1/2019 has been selected -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
</body>
function findParkHours() {
var calendarDate = $("#calendar-container").datepicker('getFormattedDate');
var hoursTable = [
["9/1/2019","USF","9a","9p"],
["9/1/2019","IOA","9a","9p"],
["9/1/2019","UVB","10a","7p"],
["9/2/2019","USF","9a","9p"],
["9/2/2019","IOA","9a","9p"],
["9/2/2019","UVB","10a","7p"],
["9/3/2019","USF","9a","9p"],
["9/3/2019","IOA","9a","9p"],
["9/3/2019","UVB","10a","6p"]
];
// iterate through rows
for (var i = 0, len1 = hoursTable.length; i < len1; i++) {
// iterate through columns
for (var j = 0, len2 = hoursTable[i].length; j < len2; j++) {
// if the cell equals the datepicker date...
if (hoursTable[i][j] === calendarDate) {
// return the next three rows to the console
for (var k = 0, len3 = 3; k < len3; k++) {
// takes i and adds k to find the next two rows in addition to i
i + k;
// writes to console the column items adjacent to j column where
// the calendar date key is stored
console.log(hoursTable[i][j+1] +
": " +
hoursTable[i][j+2] +
"-" +
hoursTable[i][j+3]);
// expected result IF 9/1/2019 is selected:
// USF: 9a-9p
// IOA: 9a-9p
// UVB: 10a-7p
}
}
}
}
}
$("#calendar-container").datepicker( {
maxViewMode: 1,
todayHighlight: true,
format: 'm/d/yyyy'
}).on('changeDate', function() {
findParkHours();
});
【问题讨论】:
-
把这个问题读3遍,请解释清楚你想要什么。
-
@Jay 对不起,我现在正在修改,我把它打了一半,更改了代码,忘记详细说明更新的场景。请参考我的编辑
-
@TonyWhite 你能添加你正在使用的日期选择器库吗?
-
@JuanS.Montoya 添加
-
@TonyWhite 添加到我的答案中,希望对您有所帮助。
标签: javascript jquery html bootstrap-datepicker