我是jquery的新手,想要从星期一开始打印所有时间(或者今天晚上==)
我设法打印对象的所有时间而不是我选择的一天,也许我的迭代是错误的,想要任何提示。
var arr = [{
"dateString": "2016-12-05",
"weekday": "mon",
"opening_hours": [{
"from": "09:00",
"to": "15:00",
"fromNumber": 9,
"toNumber": 15
}, {
"from": "17:00",
"to": "20:00",
"fromNumber": 17,
"toNumber": 20
}]
}, {
"dateString": "2016-12-06",
"weekday": "tue",
"opening_hours": [{
"from": "09:00",
"to": "17:00",
"fromNumber": 9,
"toNumber": 17
}]
}, {
"dateString": "2016-12-07",
"weekday": "wed",
"opening_hours": [{
"from": "09:00",
"to": "20:00",
"fromNumber": 9,
"toNumber": 20
}]
}, {
"dateString": "2016-12-08",
"weekday": "thu",
"opening_hours": [{
"from": "1:00",
"to": "11:00",
"fromNumber": 1,
"toNumber": 11
}]
}];
$("button").click(function() {
var today = new Date();
var str = today.toString('dddd, MMMM ,yyyy')
var sta = str.substring(0, 4);
console.log(sta);
$.each(arr, function(i, currProgram) {
$.each(currProgram.opening_hours, function(key, val) {
// $.each(num, function (axa, dasa) {
if (currProgram.weekday == "mon") {
console.log("monday");
timefrom = (val.from);
timeto = (val.to);
} else {
console.log("no monday");
}
// console.log(num[axa]);
$(".result").append("\ntime: " + timefrom + " to " + timeto + "\n");
console.log("time: " + timefrom + " to " + timeto + "\n");
// });
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="calnote">
<day></day>
<br>
<month></month>
<blackcorner>
<em>
<time></time>
</em>
</blackcorner>
</p>
<div id="div1">
<h2>click button for ajax</h2></div>
<div class="result">
<br>
</div>result</div>
<br>
<div class="result1">
</div>result1</div>
<p class="calnote">
<day></day>
<br>
<month></month>
<blackcorner>
<em>
<time></time>
</em>
</blackcorner>
</p>
click event
<button class="abc">Click me</button> here
结果应仅打印:
时间:09:00至15:00
时间:17:00至20:00
答案 0 :(得分:0)
您可能希望将逻辑console.log("time: " + timefrom + " to " + timeto + "\n");
移动到if块
var arr = [{
"dateString": "2016-12-05",
"weekday": "mon",
"opening_hours": [{
"from": "09:00",
"to": "15:00",
"fromNumber": 9,
"toNumber": 15
}, {
"from": "17:00",
"to": "20:00",
"fromNumber": 17,
"toNumber": 20
}]
}, {
"dateString": "2016-12-06",
"weekday": "tue",
"opening_hours": [{
"from": "09:00",
"to": "17:00",
"fromNumber": 9,
"toNumber": 17
}]
}, {
"dateString": "2016-12-07",
"weekday": "wed",
"opening_hours": [{
"from": "09:00",
"to": "20:00",
"fromNumber": 9,
"toNumber": 20
}]
}, {
"dateString": "2016-12-08",
"weekday": "thu",
"opening_hours": [{
"from": "1:00",
"to": "11:00",
"fromNumber": 1,
"toNumber": 11
}]
}];
function buttonClick() {
var today = new Date();
var str = today.toString('dddd, MMMM ,yyyy')
var sta = str.substring(0, 4);
$.each(arr, function(i, currProgram) {
$.each(currProgram.opening_hours, function(key, val) {
if (currProgram.weekday == "mon") {
//console.log("monday");
timefrom = (val.from);
timeto = (val.to);
console.log("time: " + timefrom + " to " + timeto + "\n");
} else {
//console.log("no monday");
}
});
});
}
buttonClick()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>