如何将“appName”和“评级”从初始点击函数传递到“解析”函数?提前感谢您的帮助。我尝试使用 data: {appName: appName, rating: rating}
但如果这确实有效,我仍然不确定如何在“解析”函数中捕获数据
<script>
$(document).ready(function(){
$('.one, .two, .three, .four, .five').click(function(){
//get the app name that's to be rated.
var appName = $(this).parents('*:eq(4)').text();
appName = appName.split("\n");
appName = appName[2]
//get the rating given to the app.
var rating = $(this).attr('class');
if(rating == 'one'){
rating = 1;
}
else if(rating == 'two'){
rating = 2;
}
else if(rating == 'three'){
rating = 3;
}
else if(rating == 'four'){
rating = 4;
}
else if(rating == 'five'){
rating = 5;
}
$.ajax({
url: 'http://xxxxxx',
dataType: 'xml',
success: parse,
error: loadfail
});
});
});
</script>
<script>
function loadfail(){
alert("Error: Failed to read file!");
}
function parse(document){
$(document).find("ViewAll").find("ROW").each(function(){
var optionLabel = $(this).find('SC_DF_FIELD_1').text();
var optionValue = $(this).find('SC_DF_FIELD_4').text();
alert(optionLabel + ":" + optionValue);
});
}
</script>
最佳答案
您可以将内联函数传递到 success
参数中,其中包含您需要的变量。
$.ajax({
url: 'http://xxxxxx',
dataType: 'xml',
success: function(result) { parse(result, appName, rating); },
error: loadfail
});
function parse(result, appName, rating) {
// etc...
}
关于javascript - 将数据从 JQuery 方法传递到 AJAX 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17574878/