简体   繁体   English

如何循环使用Javascript函数?

[英]How can I loop in a Javascript function?

I am getting trouble on looping inside the function. 我在函数内部循环时遇到麻烦。 It doesn't work I think because of my looping , I need some help correcting, please. 我认为由于循环而无法使用,请帮我纠正。 :) and suggestions of the problem :)和问题的建议

Here's what I've done: 这是我所做的:

<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>

Here's my PHP: 这是我的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">

You don't need to loop theough xhr object. 您不需要循环theough xhr对象。 Just get your response when the state is changed. 当状态改变时,只得到您的回应即可。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM