在DOM准备好的$ .each上创建函数

时间:2015-05-27 11:44:53

标签: javascript jquery

我试图迭代jsonarray,以便我可以在其他函数中使用这些值。但我不太了解如何创建这样的函数以及如何返回值。我使用以下代码:

(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
                    }
                ]
            }
        }
    }
}

完成此操作后,我使用一系列函数中的值最终填充选择框和文本字段,如下所示:

$(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 + ",-");
    });

如果需要任何其他信息,请询问。新的。 提前谢谢!

0 个答案:

没有答案