2012-07-25 75 views
0

我有以下簡單的形式從其中一個用戶可以選擇的狀態下,以查看資料:檢索在一個onchange創建一個JavaScript變量JavaScript事件

<form style="position:absolute; top:65px; left:650px"> 
    Select State to View: 
    <select id="myList" onchange="load_state_data(); xml = getStateInfo();"> 
     <option selected="selected"></option> 
     <?php 
      foreach($stateNames as $state){ 
       echo('<option>'.$state.'</option>'); 
      } 
     ?> 
    </select> 
</form> 

我的JavaScript如下:

function load_state_data() { 
    var state_name = $("#myList option:selected").val(); 
    $.ajax({ 
     type: 'post', 
     url: 'state_data.php', 
     dataType: "xml", 
     data: { 
      state_name: $("#myList option:selected").val() 
     }, 
     success: function (data) { 
      //clear out previous values 
      for (j = 0; j < 5; j++) { 
       $('#top_name_' + j).html(""); 
       $('#top_score_' + j).html(""); 
      } 

      $(data).find('TOP').each(function (index) { 
       $('#top_name_' + index).html($(this).find('COMPANY_NAME').text()); 
       $('#top_score_' + index).html($(this).find('Q_SCORE').text()); 
      }); 

      //initialize my temp arrays for reverse ordering the results 
      var botName = new Array(); 
      var botScore = new Array() 

      //clear out previous values 
      for (j = 0; j < 5; j++) { 
       $('#bot_name_' + j).html(""); 
       $('#bot_score_' + j).html(""); 
      } 

      $(data).find('BOTTOM').each(function (index) { 
       //$('#bot_name_'+index).html($(this).find('COMPANY_NAME').text()); 
       //$('#bot_score_'+index).html($(this).find('Q_SCORE').text()); 
       botName[index] = $(this).find("COMPANY_NAME").text(); 
       botScore[index] = $(this).find("Q_SCORE").text(); 
       j = index; 
      }); 
      var newOrderName = botName.reverse(); 
      var newOrderScore = botScore.reverse(); 
      for (i = 0; i < newOrderName.length; i++) { 
       $('#bot_name_' + i).html(newOrderName[i]); 
       $('#bot_score_' + i).html(newOrderScore[i]); 
      } 

      //clear the variables from memory 
      delete botName; 
      delete botScore; 
      delete newOrderName; 
      delete newOrderScore; 


      //cycle through results and save my locations to an array of the map markers 
      var inst_info = new Array(); 
      $(data).find('INST_MARKER').each(function (index) { 
       inst_info[index] = [parseFloat($(this).find('LAT').text()), 
       parseFloat($(this).find('LONG').text()), 
       $(this).find('COMPANY_NAME').text(), $(this).find('Q_SCORE').text()]; 
      }); 

      $(data).find('INST_COUNT').each(function() { 
       $('#num_total').html($(this).find('NUM_TOTAL').text()); 
       $('#num_null').html($(this).find('NUM_NULL').text()); 
      }); 
      return (inst_info); 
     }, 
     error: function (data) { 
      alert('There was an error. Please try again shortly.'); 
     } 
    }); 
}; 

我需要訪問在此腳本中生成的inst_info,它返回到它被調用的頁面上(參見上面的表格)。這是可能的,如果是這樣,怎麼樣?我曾嘗試使用inst_info =函數load_state_data,但這不起作用。

+0

[Promoted callback onSuccess返回值給調用者函數返回值](http://stackoverflow.com/questions/768457/promote-callback-onsuccess-return-value-to-the-caller-function-返回值) – 2012-07-25 22:44:47

+0

我會建議首先清理你的代碼(使用正確的縮進)並隔離問題。至少回答你的問題更容易。 – ayke 2012-07-25 22:45:24

+0

@ayke格式問題不在於我的實際代碼,而在於我無法在網站上發佈它。我第一次 - 我的歉意。 – dneimeier 2012-07-25 22:53:18

回答

1

我需要訪問該腳本中生成的inst_info,它返回到它被調用的頁面上(參見上面的表格)。這是可能的,如果是這樣,怎麼樣?

號的inst_info在您異步請求的成功事件的回調函數創建。從那裏它是可用的,你將能夠調用一個函數來設置狀態信息。

所有這些都是在您的load_state_data函數返回後的將來發生的。但是,您可能會返回數據的Promise,在數據可用後,可以在其上註冊其他回調函數。


//clear the variables from memory 
    delete ... 

此代碼不能正常工作,它是delete operator的濫用。無論如何,您不需要這樣做,JavaScript是一種垃圾收集語言。

2

這是不可能的。事件處理程序在某個時間點異步觸發,並且沒有代碼將值「返回」。無論發生什麼事件處理程序load_state_data()都與您的主要JavaScript代碼隔離,因爲沒有人知道什麼時候會發生,甚至是否會發生。 你必須做任何你想做的事情處理程序。

+0

謝謝。我很害怕這個。我將移動代碼並在我的腳本中添加一個ajax調用,這將完成我的工作。 – dneimeier 2012-07-25 23:27:48

相關問題