我怎样才能循环Javascript函数?

时间:2016-02-17 05:53:00

标签: javascript php

我在函数内部循环时遇到麻烦。它不起作用我认为因为我的循环,我需要一些帮助纠正,请。 :)和问题的建议

这就是我所做的:

<script>
      function getImage1(str) {


        if (window.XMLHttpRequest) {
          // code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
        } else { // code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function() {
        for (i = 0; i < xmlhttp.length; i++) {
          if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("result1").innerHTML=xmlhttp.responseText;

          }
        }
        xmlhttp.open("GET","getImage.php?no="+str,true);
        xmlhttp.send();
        }
      }

   </script>

这是我的PHP:

<select  onchange="getImage1(this.value)" name="<?php echo $rowasa['pos_name'] ?>" style="height: 47px;">
        <option value='0' >Pls. Select <?php echo $rowasa['pos_name'] ?></option>
        <?php
        include('connection/connect.php');
        $dsds=$rowasa['posid'];
        $YearNow=Date('Y');
            $results = $db->prepare("SELECT * FROM candidates,student,school_year,partylist where student.idno = candidates.idno AND school_year.syearid = candidates.syearid AND posid =:a AND candidates.partyid = partylist.partyid   ");

            $results->bindParam(':a', $dsds);
            $results->execute();
            while($rows = $results->fetch()){
                            ?><!---$rows['candid'] . "," .-->
                    <option style="padding: 35px 50px 35px 80px; background:url('admin/candidates/images/<?php echo $rows['image']; ?>')  no-repeat scroll 5px 7px / 70px auto rgba(0, 0, 0, 0);"  

                value="<?php echo $rows['candid'] . "-" ."&nbsp". $rows['lastname'] .",". "&nbsp". $rows['firstname'] ?>"> <?php echo $rows['lastname'] ?>,
                    <?php echo $rows['firstname'] ?>

                - <?php 
                        echo $rows['party_name']?></option>

                <?php

            }

        ?>
            </select>
            <div class="12u$"><span id="result1">

1 个答案:

答案 0 :(得分:0)

您不需要循环theough xhr对象。只需在状态发生变化时获得您的回复。

<script>
      function getImage1(str) {


        if (window.XMLHttpRequest) {
          // code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
        } else { // code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function() {           
          if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("result1").innerHTML=xmlhttp.responseText;    
          }

        xmlhttp.open("GET","getImage.php?no="+str,true);
        xmlhttp.send();
        }
      }

   </script>