<?       
$db=   mysql_connect( "localhost ", "root ", "java "); 
mysql_select_db( "ajax ",$db);     
      
?>       
<!DOCTYPE   html   PUBLIC   "-//W3C//DTD   XHTML   1.0   Transitional//EN "   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">       
<html   xmlns= "http://www.w3.org/1999/xhtml ">       
<head>       
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 "   />       
<title> 无标题文档 </title>   
<script   type= "text/javascript "   language= "JavaScript "> 
<!-- var   xmlHttp;   
function   createXMLHttpRequest(){   
if(window.XMLHttpRequest){   
xmlHttp   =   new   XMLHttpRequest();   
}else   if(window.ActiveXObject){   
try{   
xmlHttp   =   new   ActiveXObject( "Msxml2.XMLHTTP ");   
}catch(e){   
try{   
xmlHttp   =   new   ActiveXObject( "Microsoft.XMLHTTP ");   
}catch(e){   
          }   
        }   
    }   
}   function   addSelect(sid,elementID)   { 
  //   alert(elementID);有值 
    //   alert(sid);有值 
    oElement=document.getElementById(elementID);     
    //alert(oElement);//没值。 
    
    createXMLHttpRequest();   
  var   url   =   "select.php?sid= "   +   sid; 
  alert(url); 
  xmlHttp.onreadystatechange     =     function(){onStateChange(oElement);} 
alert( "aa ");//到这不执行了 
  xmlHttp.open( "GET ",   url,   true); 
  xmlHttp.send(null); 
}   
function   onStateChange(oElement){ 
    
        if(xmlHttp.readyState   ==   4){ 
                if(xmlHttp.status   ==   200){ 
    var   returntxt=unescape(xmlHttp.responseText); 
    var   htmltxt   =   ' <select   name= "city "   id= "city "> '   +   returntxt   +   ' </select> '; 
      document.getElementById( "citybox ").innerHTML=htmltxt; 
                } 
        } 

//--> 
</script>   </head>       
<body>       
<table   width= "100% "   border= "0 "   cellspacing= "2 "   cellpadding= "3 "   class= "formtbl ">       
        <form   name= "regtbl "   id= "regtbl ">                             
                <tr>       
                        <td> <div   align= "right "> 所在城市: </div> </td>       
                        <td> <div   style= "float:left ">       
                                        <select   name= "province "   size= "1 "   id= "province "   onchange= "addSelect(this.options[this.selectedIndex].value, 'city '); ">       
                                                <option   value= " "> 选择省份... </option>       
                                              
<?php                                                   
                              $result   =   mysql_query( "select   *   from   gt_province ");   
                                while($row   =   mysql_fetch_array($result)){       
                                        echo   " <option   value=\ " ".$row[ 'proid ']. "\ "> ".$row[ 'proname ']. " </option> ";                                         
                                }         
                        ?>       
                                        </select>       
                                </div>       
                                <div   id= "citybox "   style= "float:left;   padding-left:5px; "> </div> </td>       
                </tr>     
        </form>       
</table>       
</body>       
</html>       
为什么上面的代码到xmlHttp.onreadystatechange     =     function(){onStateChange(oElement);} 
不执行了,还有oElement=document.getElementById(elementID);   这地方没值哦.加个.value也不对哦. 

解决方案 »

  1.   

    xmlHttp.onreadystatechange           =           function(){onStateChange(oElement);}   
    alert(   "aa   ");//到这不执行了 ——————————————————————————————————————]
    看看是不是function(){onStateChange(oElement);}   ——————————————————
    还有oElement=document.getElementById(elementID);      没值  returntxt=unescape(xmlHttp.responseText);   这个好象不对
    应是xmlHttp.responsexml
    好象dom只能解析xml,不能解析html
      

  2.   

    xmlHttp.responsexml 
    能将xml格式化为dom
    这样才能操作,不能格式化为dom的不能操作
      

  3.   

    看看《ajax基础教程》,csdn应有下载
    其次看一下javacript的dom部分,