<SCRIPT LANGUAGE=javascript>
  //定义一个二维数组aArray,用于存放城市名称。
  var aCity=new Array();
  aCity[0]=new Array();
  aCity[1]=new Array();
  aCity[2]=new Array();
  aCity[3]=new Array();
  //赋值,每个省份的城市存放于数组的一行。
  aCity[0][0]="--请选择--";
  aCity[1][0]="--请选择--";
  aCity[1][1]="广州市";
  aCity[1][2]="深圳市";
  aCity[1][3]="珠海市";
  aCity[1][4]="汕头市";
  aCity[1][5]="佛山市";
  aCity[2][0]="--请选择--";
  aCity[2][1]="长沙市";
  aCity[2][2]="株州市";
  aCity[2][3]="湘潭市";
  aCity[3][0]="--请选择--";
  aCity[3][1]="杭州市";
  aCity[3][2]="苏州市";
  aCity[3][3]="温州市";
  function ChangeCity()
  {
  var i,iProvinceIndex;
  iProvinceIndex=document.frm.optProvince.selectedIndex;
  iCityCount=0;
  while (aCity[iProvinceIndex][iCityCount]!=null)
  iCityCount++;
  //计算选定省份的城市个数
  document.frm.optCity.length=iCityCount;//改变下拉菜单的选项数
  for (i=0;i<=iCityCount-1;i++)//改变下拉菜单的内容
  document.frm.optCity[i]=new Option(aCity[iProvinceIndex][i]); 
  document.frm.optCity.focus();
  } 
  </SCRIPT>  <H3>选择你所在的省份及城市</H3>
  <FORM NAME="frm">
  <P>省份:
  <SELECT NAME="optProvince" SIZE="1" ONCHANGE=ChangeCity()>
  <OPTION>--请选择--</OPTION>
  <OPTION>广东省</OPTION>
  <OPTION>湖南省</OPTION>
  <OPTION>浙江省</OPTION>
  </SELECT>
  </P>
  <P>城市:
  <SELECT NAME="optCity" SIZE="1">
  <OPTION>--请选择--</OPTION> 
  </SELECT>
  </P>
  </FORM>
以上静态数据能实现,php读取数据就不会了

解决方案 »

  1.   

    首先我们来在数据库里面建一张名为city的表,SQl语句如下:
    CREATE TABLE `city` (
      `ID` int(3) NOT NULL auto_increment COMMENT '自增ID',
      `parentID` int(3) NOT NULL COMMENT '父ID',
      `title` varchar(50) character set gbk collate gbk_bin NOT NULL COMMENT '省份或是城市',
      PRIMARY KEY  (`ID`)
    );-- 
    -- 导出表中的数据 `city`
    -- INSERT INTO `city` (`ID`, `parentID`, `title`) VALUES 
    (1, 0, 江苏),
    (2, 0, 广东),
    (3, 2, 珠海),
    (4, 2, 广州),
    (5, 1, 苏州),
    (6, 1, 南京);请看index.php页面的代码:<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
    <title>Ajax联动菜单</title> 
    </head> 
    <body> 
    <?php
    mysql_connect("localhost", "root", "123");
    mysql_select_db("city");
    mysql_query("set names gb2312");
    $sql = "Select * From city where parentID=0";
    $result = mysql_query($sql);
    ?>
    <form name="form1" method="post" action=""> 
        <select name="select1" id="select1" onChange="startRequest('select2',this.value)"> 
    <option value=0>省份</option>
    <?php
    while($arr = mysql_fetch_array($result))
    {
    ?>
          <option value="<?php echo $arr['ID'];?>"><?php echo $arr['title'];?></option>
      <?php
       }
      ?>
        </select> 
    </p>
      <p> 
        <select name="select2" id="select2">
    <option>城市</option>
        </select> 
        
    </form> 
    <script language="javascript"> 
    //var ab = new Array(); 
    var xmlHttp; function createXMLHttpRequest() { 
        if (window.ActiveXObject) { 
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
        }  
        else if (window.XMLHttpRequest) { 
            xmlHttp = new XMLHttpRequest(); 
        } 

    var tag;
    function startRequest(item,url) { 
        createXMLHttpRequest(); 
        xmlHttp.onreadystatechange = handleStateChange; 
        document.getElementById(item).options.length = 1;
        //var url = document.form1.select1.value; 
    //var qurl = "getCities.php?countryCode="+url; 
    var qurl = "getCities.php?id="+url+"&time="+new Date().getTime();
    tag=item;
        xmlHttp.open("GET", qurl, true); 
        xmlHttp.send(null); 
        //setTimeout("startRequest()",2000); 

         
    function handleStateChange() { 
        if(xmlHttp.readyState == 4) { 
            if(xmlHttp.status == 200) { 
    var obj = document.getElementById(tag);
    if(xmlHttp.responseText.length==0) {
    obj.style.display="none";
    }
    else{
    obj.style.display="";
    eval(xmlHttp.responseText); 
    }
            } 
        } 

    </script> 
    </body> 
    </html> 再请看getCities.php的代码:
    <?php 
    header("Content-type:text/html; charset=gb2312");
    if(isset($_GET['id'])){
    mysql_connect("localhost", "root", "123");
    mysql_select_db("city");
    mysql_query("set names gb2312");
    $id = $_GET['id'];
    $sql = "select * from city where parentID = ".$id;
    $result = mysql_query($sql);
    while($arr = mysql_fetch_array($result))
    {
    echo "obj.options[obj.options.length] = new Option('".$arr["title"]."','".$arr["ID"]."');\n"; 
    }

    ?>最后运行一下index.php看下效果,呵呵,成功了
      

  2.   

    楼主用这个SQL语句吧,我去掉了那个title的编码。CREATE TABLE `city` (
      `ID` int(3) NOT NULL auto_increment COMMENT '自增ID',
      `parentID` int(3) NOT NULL COMMENT '父ID',
      `title` varchar(50) NOT NULL COMMENT '省份或是城市',
      PRIMARY KEY  (`ID`)
    );-- 
    -- 导出表中的数据 `city`
    -- INSERT INTO `city` (`ID`, `parentID`, `title`) VALUES 
    (1, 0, 江苏),
    (2, 0, 广东),
    (3, 2, 珠海),
    (4, 2, 广州),
    (5, 1, 苏州),
    (6, 1, 南京);
      

  3.   

    http://topic.csdn.net/u/20080425/10/509a1dc9-8187-4dab-ab6d-f1078031cdcf.html