我有以下簡單的形式從其中一個用戶可以選擇的狀態下,以查看資料:檢索在一個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,但這不起作用。
[Promoted callback onSuccess返回值給調用者函數返回值](http://stackoverflow.com/questions/768457/promote-callback-onsuccess-return-value-to-the-caller-function-返回值) – 2012-07-25 22:44:47
我會建議首先清理你的代碼(使用正確的縮進)並隔離問題。至少回答你的問題更容易。 – ayke 2012-07-25 22:45:24
@ayke格式問題不在於我的實際代碼,而在於我無法在網站上發佈它。我第一次 - 我的歉意。 – dneimeier 2012-07-25 22:53:18