0

我在下面看到了类似的问题: 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>
4

1 回答 1

0

我强烈建议您查看 jQuery.load:http ://api.jquery.com/load/ 。您尝试做的事情并不像您编写的代码那么复杂。

我不会在这里保证准确的语法,但这里是您上面使用 jQuery 和 jQuery 加载重新编写的 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.load 方法有几个选项可以让您响应完成,因此请阅读文档。

于 2013-03-12T19:27:04.103 回答