doumengyin0491 2012-05-09 20:13
浏览 52
已采纳

Ajax有5个下拉列表

I have a form that contains 5 drop down lists which are created by PHP querying a MySql database. The lists are being built correctly.

I want the user to be able to select from a list and have it fill in the form at the bottom based on the value selected.

It works find for the 1st list.

Here's the code from list 1

<td width="90">
<p><select size="1" name="D1" onchange="showStudent(this.value);"> 
 <?php while(list($id, $student_id)=mysql_fetch_row($result1)) {
 echo "
<option value=\"".$student_id."\">".$student_id."</option>";
 }
 ?>
 </select></p>
 </td>

Here's the code From list 2

<td>
<p><select size="1" name="D2" onchange=”showStudent(this.value);” >
<?php while(list($id, $student_id)=mysql_fetch_row($result2)) {
echo "
<option value=\"".$student_id."\">".$student_id."</option>";
 }
 ?>
</select></p>
</td>

Here's the javascript code

<script type="text/javascript">
function CreateXmlHttpObject() { //function to return the xml http object
    var xmlhttp=false;  
    try{
        xmlhttp=new XMLHttpRequest();//creates a new ajax object
    }
    catch(e)    {       
        try{            
            xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");//this is for IE browser
        }
        catch(e){
            try{
            req = new ActiveXObject("Msxml2.XMLHTTP");//this is for IE browser
            }
            catch(e1){
                xmlhttp=false;//error creating object
            }
        }
    }

    return xmlhttp;
}

function showStudent(str)
{
//  alert("Made it to show students"+ str);
if (str=="")
{
document.getElementById("student_data").innerHTML="";
 return;
}
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("student_data").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","editstudent.php?d="+str,true);
xmlhttp.send();
}

</script>

If I select from the 1st list, everything works as it should. If however I select from lists 2-5, nothing. Even if I select from one of them first. I have even tried changing the name of the function to match a specific list name and still only the 1st one works.

What am I missing?

  • 写回答

1条回答 默认 最新

  • douzong5473 2012-05-09 20:28
    关注

    I believe it's because you have the wrong double-quote tags around your onchange in the html for the 2nd list? =”showStudent(this.value);” > should be ="showStudent(this.value);" >

    These odd quote tags come often from copy/pasting from word, outlook or other office applications. They are easy to overlook!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机数字电压表电路组成及框图
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line