一款聯動菜單,利用了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, ''); // 選項計數從 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> 三級