澳门新葡萄京官网首页php+ajax无刷新实现省、地、市三级联动二

?$conn=mysql_connect(localhost,root,2328725);mysql_select_db(novel);mysql_query(setnames’utf8′);if($provincecode!=$provincecode!=650000$provincecode!=710000$provincecode!=810000$provincecode!=820000$provincecode!=110000$provincecode!=120000$provincecode!=310000$provincecode!=500000){$sql=select*fromcitywhereprovincecode=$provincecode;$result=mysql_query($sql);echoselectonchange=’queryArea(this.options[this.selectedIndex].value)’/n;echooptionvalue=’-1’selected请选择城市/option/n;while($row=mysql_fetch_row($result)){echooptionvalue=’$row[1]’$row[2]/option/n;}echo/select/n;}if($citycode!=){$sql=select*澳门新葡萄京官网首页 ,fromareawherecitycode=$citycode;$result=mysql_query($sql);echoselect/n;echooptionvalue=’-1’selected请选择县/option/n;while($row=mysql_fetch_row($result)){echooptionvalue=’$row[1]’$row[2]/option/n;}echo/select/n;}?

 代码如下

<script language=”JavaScript”
type=”text/网页特效”>
function
removeOptions(selectObj){    
          if (typeof selectObj != ‘object’){
            selectObj = document.getElementById(selectObj);
    }      // 原有选项计数    
          var len = selectObj.options.length;    
    for (var i=0; i < len; i++){         // 移除当前选项        
        selectObj.options[0] = null;    
    }
}
function setSelectOption(selectObj, optionList, firstOption, selected)
{   
     if (typeof selectObj != ‘object’){        
      selectObj = document.getElementById(selectObj);
  }      // 清空选项    
  removeOptions(selectObj);      // 选项计数    
  var start = 0;          // 如果需要添加第一个选项    
  if (firstOption){        
       selectObj.options[0] = new Option(firstOption, ”);          //
选项计数从 1 开始        
    start ++;   
  }     
  var len = optionList.length;
  for (var i=0; i < len; i++){         // 设置 option        
      selectObj.options[start] = new Option(optionList[i].txt,
optionList[i].val);          // 选中项        
   if(selected == optionList[i].val){            
       selectObj.options[start].selected = true;        
   }          // 计数加 1        
  start ++;    
 } 
}  //–>
 var sidArr = [];

 <?PHP

 

$sql=”select * from ptypes where sid = 0 and ssid =0″;
$result=mysql教程_query($sql,$conn)
or
die(“查询数据库教程出错1”);
while($row=mysql_fetch_object($result))
{
   $i=0;
   $rs2=””;
   $sql2=”select * from ptypes where sid='”.$row->id.”‘”;
   $result2=mysql_query($sql2,$conn) or die(“查询数据出错2”);
   while($row2=mysql_fetch_object($result2))
   {
       if ($i==0){
        $t=””;
    }else{
        $t=”,”;
    }
       $rs2.=$t.”{txt:'”.$row2->title.”‘,val:'”.$row2->id.”‘}n”;
    $n=0;
    $rs3=””;
    $sql3=”select * from ptypes where ssid='”.$row2->id.”‘”;
    $result3=mysql_query($sql3,$conn) or die(“查询是数据库出错3”);
    while($row3=mysql_fetch_object($result3))
    {
       if ($n==0)
    {
       $t1=””;
    }else{
       $t1=”,”;
    }
    $rs3.=$t1.”{txt:'”.$row3->title.”‘,val:'”.$row3->id.”‘}n”;
    $n=1;
    }
   
    echo(“sidArr[‘”.$row2->id.”‘]=[“.$rs3.”];”);
    $i=1;
   }
  
   echo(“sidArr[‘”.$row->id.”‘]=[“.$rs2.”];”);
}
?>
function setCity(province) {
       
         removeOptions(document.getElementById(‘sid’));
   removeOptions(document.getElementById(‘ssid’));
   setSelectOption(‘sid’, “”, ‘-请选择-‘);
   setSelectOption(‘ssid’, “”, ‘-请选择-‘);
         if (!sidArr[province] || sidArr[province]==””){
      
             document.getElementById(‘sid’).disabled=true;
    document.getElementById(‘ssid’).disabled=true;
   }else{
       document.getElementById(‘sid’).disabled=false;
    document.getElementById(‘ssid’).disabled=false;
             setSelectOption(‘sid’, sidArr[province], ‘-请选择-‘);
   }
}
function setssid(province) {
         removeOptions(document.getElementById(‘ssid’));
   setSelectOption(‘ssid’, “”, ‘-请选择-‘);
         if (!sidArr[province] || sidArr[province]==””){
    document.getElementById(‘ssid’).disabled=true;
   }else{
       document.getElementById(‘ssid’).disabled=false;
             setSelectOption(‘ssid’, sidArr[province], ‘-请选择-‘);
   }
}
</script>

<select name=”province” id=”province” onchange=”if(this.value != ”)
setCity(this.options[this.selectedIndex].value);”>  
   <option value=””>-请选择-</option>
   <?php教程
   $sql=”select * from ptypes where sid=0 and ssid = 0″;
   $result=mysql_query($sql,$conn) or die(“返回数据记录出错!”);
   while($row=mysql_fetch_object($result))
   {
       echo (“<option
value='”.$row->id.”‘>”.$row->title.”</option>”);
   }
   ?>
</select> 一级
  
<select name=”sid” id=”sid” onchange=”if(this.value != ”)
setssid(this.options[this.selectedIndex].value);” >    
<option value=””>-请选择-</option>
</select> 二级

<select name=”ssid” id=”ssid”>    
<option value=””>-请选择-</option>
</select> 三级

发表评论

电子邮件地址不会被公开。 必填项已用*标注