我在下面看到了类似的问题:2 AJAX命令工作,但我只看到最后的"responseText"
但是我正在使用字段数组,所以我不确定这是否使它不同。为了进行测试,我将这个添加到顶部来定义一些数据:
<?php
$a = array('star_wars','marvel','board_games','','The_Han_Solo_Omnibus');
$b = implode("~",$a);
$count = count($a);
?>
我看到在上面的答案他定义:var httpRequest;解决问题。我尝试- var xmlhttp;但这并没有成功。
关于如何让所有的部分加载,而不仅仅是最后一个有什么想法吗?我甚至添加了大量的延迟,但什么都不起作用。
感谢<script>
function showDiv(a)
{
var temp = new Array();
var delay = 2000; // 1 second
var result = 'loading'; // the result from your AJAX response
var xmlhttp;
temp = a.split('~');
for(i=0;i<temp.length;i++)
{
alert(temp[i]);
divno = i + 1;
currentdiv = "loadArea" + divno;
alert (currentdiv);
//var pageNumber = 1;
//eval("var text" + pageNumber + "=123;");
//alert(text1);
if (temp[i]=="")
{
document.getElementById(currentdiv).innerHTML=""; //increment
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();//increment
alert ("usingchrome");
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");//increment
//alert ("using IE");
}
xmlhttp.onreadystatechange=setTimeout(function() //increment
{
alert ("inreadystate");
if (xmlhttp.readyState==4 && xmlhttp.status==200) //increment
{
if (temp[i] !="") {
setTimeout(function() {
document.getElementById(currentdiv).innerHTML=xmlhttp.responseText; //increment
}, delay);
alert ('valueisgood');
}
}
}, delay);
if (temp[i] !="") {
url = "http://zocreative.com/load.php?f="+temp[i];
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
}
}
</script>
</head>
<body id="page1" onload="showDiv('<?php echo $b; ?>');">
<?php
$i = 1;
while ($i <= $count) {
echo ("<div id='loadArea". $i ."'><br />
<!-- AJAX content will load here-->
loading ... ". $i ."
</div>");
/* the printed value would be
$i before the increment
(post-increment) */
$i++;
}
?>
<div id="note"></div>
我强烈建议您看看jQuery。负载:http://api.jquery.com/load/。你想做的事情并不像你写的代码那么复杂。
我在这里不保证精确的语法,但这里是你使用jQuery和jQuery load重新编写的Javascript代码的简化:
function showDiv(a) {
var temp = a.split('~'),
divno;
for(i = 0; i < temp.length; i += 1) {
divno = i + 1;
jQuery('#loadArea' + divno.toString()).load('http://zocreative.com/load.php?f=' + temp[i]);
}
这里的关键是,您要求jQuery将URL加载到指定的div中,而不必自己跟踪异步响应。jQuery有几个选项。方法,它将允许您响应完成,因此请阅读文档。