- <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', '-请选择-');
- }
- }
- </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> 三级