加载step-02.php文件后如何使用变量“ hotel”?

    $(function () {
    var hotel = [];
    $('#forPrice').on('click', function() {
        $('select').each(function() {
            if($(this).val() > 0 && $(this).val() < 10) {
                var room = {};
                room["room"] = $(this).attr('room');
                room["price"] = $(this).attr('price');
                room["val"] = $(this).val();
                room["html"] = $(this).parents(".selectRowRoom").html();

                hotel.push(room);
            }
        });
        console.log(hotel);
    });

    $('#forPrice').click(function () {
        $('#Step-01').hide();
        $('#Step-02').show();
        $('.hiddenAndShow').hide();
        $( "#Step-02" ).load( 'step_02.php' );
    });
})


我需要


  room [“ html”] = $(this).parents(“。selectRowRoom”)。html();


在加载step-02.php之后显示。

step-02.php

<div class="thisRoom">
    // Insert room["html"] = $(this).parents(".selectRowRoom").html();
</div>

最佳答案

jQuery load()$.ajax的快捷方式,它是异步的,您必须等待结果能够访问它。

幸运的是,load()有一个回调,该回调在插入内容并准备访问时触发

$(function() {
    var hotel = [];
    $('#forPrice').on('click', function() {
        $('select').each(function() {
            if ($(this).val() > 0 && $(this).val() < 10) {
                var room = {};
                room["room"] = $(this).attr('room');
                room["price"] = $(this).attr('price');
                room["val"] = $(this).val();
                room["html"] = $(this).parents(".selectRowRoom").html();

                hotel.push(room);
            }
        });
        console.log(hotel);
    });

    $('#forPrice').click(function() {
        $('#Step-01').hide();
        $('#Step-02').show();
        $('.hiddenAndShow').hide();
        $("#Step-02").load('step_02.php', function() { // callback here
            $("#Step-02").find('.thisRoom').html(JSON.stringify(hotel));
        });
    });
});

10-05 20:42
查看更多