本文介绍了jQuery中的$ .each()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过数组jQuery.each和数据hotel_id从数组中追加一些,每个hotel_id的编号为4,并且此循环$.each(data[0].hotel_id,... });在self内运行4次内容,以防在数据库行中存在或插入数据库行residence_uresidence_p 3次(每个都有3次,而不是4次(例如hotel_id)),当运行以下代码时,此代码没有错误,这是行不通的.如果我删除代码$.each(info_ru.units,... });$.each(info_rp.start_date,... });,它起作用了:如何解决?

I want append some from array by jQuery.each and data hotel_id, number each hotel_id is 4, and this loop $.each(data[0].hotel_id,... }); run 4 times contents inside self, in case that there are or inserted in database rows residence_u and residence_p 3 times (each of they there are 3, not 4(like hotel_id)), when run the following code, this code not have error jus not work. if i remove codes $.each(info_ru.units,... }); and $.each(info_rp.start_date,... }); it worked: How can fix it?

查看我的完整js代码:[ http://pastebin.com/jBWEDZrN ] [1]

See full my js code: [http://pastebin.com/jBWEDZrN][1]

这是来自我在ajax调用(url: 'get_gr',)中的js代码的摘要:

This is summary from my js code in ajax call(url: 'get_gr',):

$.each(data[0].hotel_id, function (index, value) {
    var $li = $('<li><input name="hotel_id[]" value="' + value.hotel_id + '" style="display:none;"></li>');
    var $tooltip = $('<div class="tooltip"></div>').appendTo($li);
    $li.appendTo('#residence_name');

    var info_ru = data[0].residence_u[index];
    $.each(info_ru.units, function (index, value) {
        $tooltip.append(value + ' & ' + info_ru.extra[index] + ' & ' + info_ru.price[index] + '<br>');
    });

    var info_rp = data[0].residence_p[index];
    $.each(info_rp.start_date, function (index, value) {
        $tooltip.append(value + ' & ' + info_rp.end_date[index] + ' & ' + info_rp.price_change[index] + '<br>');
    });
    tool_tip()
});

这是输出的php代码(url: 'get_gr',):

This is output php code(url: 'get_gr',):

[{
    "guide": null,
    "hotel_id": [{
        "hotel_id": ["1"]
    }, {
        "hotel_id": ["2"]
    }, {
        "hotel_id": ["3"]
    }, {
        "hotel_id": ["4"]
    }],
    "residence_u": [{
        "units": ["hello", "how", "what"],
        "extra": ["11", "22", "33"],
        "price": ["1,111,111", "2,222,222", "3,333,333"]
    }, {
        "units": ["fine"],
        "extra": ["44"],
        "price": ["4,444,444"]
    }, {
        "units": ["thanks", "good"],
        "extra": ["55", "66"],
        "price": ["5,555,555", "6,666,666"]
    }],
    "residence_p": [{
        "start_date": ["1111", "2222"],
        "end_date": ["1111", "2222"],
        "price_change": ["1111", "2222"]
    }, {
        "start_date": ["3333", "4444"],
        "end_date": ["3333", "4444"],
        "price_change": ["3333", "4444"]
    }, {
        "start_date": ["5555", "6666"],
        "end_date": ["5555", "6666"],
        "price_change": ["5555", "6666"]
    }]
}]

更新:

我在数据库表的一行中用json_encode插入了多个(array)值,现在想用jquery按顺序回显它们.

I insert several(array) value with json_encode in one row from database table, now want echo they as order with jquery.

输出js代码是这样的:

Output js code is this:

推荐答案

您的Residence_u中只有3个元素,而Residence_p数组中只有2个元素,而原始酒店数组中只有4个元素.就像检查有效的info_ru一样简单

You only have 3 elements in your residence_u and 2 in residence_p arrays, but 4 in the original hotels array. Could it be as simple as to check for a valid info_ru

var info_ru = data[0].residence_u[index];
if ( infor_ru != null ) {
    $.each(info_ru.units, function (index, value) {
        $tooltip.append(value + ' & ' + info_ru.extra[index] + ' & ' + info_ru.price[index] + '<br>');
    });
}

在继续每个之前?

这篇关于jQuery中的$ .each()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 23:04