繁体   English   中英

jQuery中的$ .each()问题

[英]Problem with $.each() in jQuery

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

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

这是我在ajax调用中的js代码的摘要( 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', ):

[{
    "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插入了json_encodearray )值,现在想用jquery将它们按顺序回显。

输出js代码是这样的:

1
hello111,111,111
how222,222,222
what333,333,333,
111111111111
222222222222

2
fine444,444,444
333333333333
444444444444

3
thanks555,555,555
good666,666,666
555555555555
666666666666

4

您的Residence_u数组中只有3个元素,而Residence_P数组中只有2个元素,而原始酒店数组中只有4个元素。 就像检查有效的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>');
    });
}

在继续每个之前?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM