一款联动菜单,利用了js php mysql实现的无刷新效果,有需要的朋友可以直接下载哦。
- <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, '');
- start ++;
- }
- var len = optionList.length;
- for (var i=0; i < len; i++){
- selectObj.options[start] = new Option(optionList[i].txt, optionList[i].val);
- if(selected == optionList[i].val){
- selectObj.options[start].selected = true;
- }
- 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> 三级