我有一个简单的 js 函数,我在其中传递迭代次数。 这适用于 1 次迭代,如果它超过 1,它会在警报中显示未定义。 如何将 test_iterations 存储在 ajax 函数中。
function _startTest(test_iterations) {
$.ajax({
url: '<?php echo $_SERVER['PHP_SELF']; ?>?start=1',
success: function(data) {
$('.results').append('<p>Test #' + test_number +' ' + data + ' ms</p>');
$('.results').scrollTop(10000);
test_results.push(data);
alert("_startTest" + test_iterations);
if(test_number >= test_iterations) {
end_tests(test_iterations);
}
test_number++;
}
});
}
最佳答案
不确定这个问题,但是您是否尝试过在函数之外放置 var ?看来您一直在使用 test_number
var iteration_count = 0; // here
function _startTest(test_iterations) {
iteration_count++; // here
$.ajax({
url: '<?php echo $_SERVER['PHP_SELF']; ?>?start=1',
success: function(data) {
$('.results').append('<p>Test #' + test_number +' ' + data + ' ms</p>');
$('.results').scrollTop(10000);
test_results.push(data);
alert("_startTest" + iteration_count); // use this value instead
if(test_number >= test_iterations) {
end_tests(test_iterations);
}
test_number++;
}
});
}
关于php - Ajax + 变量未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14889430/