Javascript 在DOM ready上使用$.each创建函数
我试图迭代jsonarray,以便在其他函数中使用这些值。但我不太明白如何创建这样的函数以及如何返回值。我正在使用以下代码:Javascript 在DOM ready上使用$.each创建函数,javascript,jquery,Javascript,Jquery,我试图迭代jsonarray,以便在其他函数中使用这些值。但我不太明白如何创建这样的函数以及如何返回值。我正在使用以下代码: (function($) { $.fn.loopThroughCurrent = function() { console.log(currentJson); $.each(currentJson, function(k,v) { firstTypeValue= k; console.
(function($) {
$.fn.loopThroughCurrent = function() {
console.log(currentJson);
$.each(currentJson, function(k,v) {
firstTypeValue= k;
console.log(firstTypeValue);
$.each(v, function(k,v) {
secondTypeValue= k;
console.log(secondTypeValue);
$.each(v, function(k,v) {
year = k;
console.log(year);
$.each(v, function(k,v) {
month = k;
console.log(month);
$.each(v, function(k,v) {
id = v.id;
startDate = v.startDate;
endDate = v.endDate;
price = v.price;
console.log('id: ' + id);
console.log('startDate: ' + startDate);
console.log('endDate: ' + endDate);
console.log('price: ' + price);
})
})
})
})
});
return ?
};
}(jQuery));
$(document).ready().loopThroughCurrent();
以下json数组:
{
"Clienten en patienten": {
"Weekend": {
"2015": {
"05": [
{
"id": 105,
"startDate": "01-05-2015",
"endDate": "04-05-2015",
"price": 645
}
]
}
}
}
}
完成后,我将使用一系列函数中的值最终填充SelectBox和textfields,如下所示:
$(document)
.ready(changeFirstType(firstTypeValue))
.ready(changeSecondType(secondTypeValue))
.ready(getMonths(year))
.ready(getDates(month)).ready(function() {
$("select[name='year'] option:eq(1)").attr('selected', 'selected');
$("select[name='month'] option:eq(1)").attr('selected', 'selected');
$("select[name='startDate'] option:eq(1)").attr('selected', 'selected');
$("input[type='radio'][name='dateType'][value='"+ secondTypeValue+"']").prop('checked', true);
})
.ready(getEndDate()).ready(function() {
$(endDateText).val(" tot: " + endDate);
$(priceText).val(" \u20AC " + price + ",-");
});
如果需要任何其他信息,请询问。这是新的。
提前谢谢首先,你有回叫地狱,然后你有链式就绪的调用,不做任何事情,因为函数被调用,而不是引用,它不能得到任何陌生人比这一点。如果你有这么多嵌套循环,你应该真的考虑递归函数。@ AdENOO就像我说的,我是新的。最后一部分是现有的、正在运行的更改函数。如果做得不对,我也会调查一下。我主要感兴趣的是如何创建完成第一部分的函数。