2016-10-08 5 views
2

step-02.php 파일을로드 한 후에 변수 "hotel"을 사용하는 방법은 무엇입니까?jQuery에서 PHP 파일을로드 한 후 함수에서 변수를 사용하는 방법은 무엇입니까?

$(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'); 
    }); 
}) 

제가

실 [ "HTML"] = $ (이) .parents (". selectRowRoom")를 필요로 HTML을().;

로드 후 단계 - 02.php.

step-02.php

:

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

에 액세스하기위한 삽입 준비가 화재 콜백을 가지고? – xzegga

+0

@xzegga 안녕하세요.이 질문이 맞습니다. – mySun

+0

그래서'.thisRoom'이 step_02.php에 있는지 또는 무엇을 말하고 싶습니까? – adeneo

답변

1

jQuery를 load()가 비동기 $.ajax에 대한 바로 가기입니다, 당신은 그것을 액세스 할 수 있도록 결과를 기다려야한다. 다행히

load()는 내용이 어디 PHP 파일을로드

$(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)); 
     }); 
    }); 
}); 
1

글로벌 hotel를 확인하고 데이터 ATTR의 방 키 번호를 저장합니다. 이처럼 :

$(function() { 
    window.hotel = []; // new 
    $('#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(); 

       $(this).data("numRoom", hotel.length); // new 
       hotel.push(room); 
      } 
     }); 
     console.log(hotel); 
    }); 

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

//After : 
hotel[$(this).data("numRoom")]["html"] = $(this).parents(".selectRowRoom").html(); 
관련 문제