本文目錄一覽:
php二級聯動菜單
我給你提供一個三級的
主頁面是
!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”
html xmlns=””
head
title三級聯動下拉列表/title
meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /
script type=”text/javascript” src=”/js/jquery.js”/script
script type=”text/javascript”
!–
var temp;
var ids = [‘province’,’city’,’eare1′]; //默認要操作的三個ID,注意先後順序,不可顛倒。
// 參數說明:pid是關聯 的id (第二個參數) 的父級,n表示是第幾級,(如第一級,第二級,第三級),selected 默認被選中的選擇的主鍵
function getList (pid,id,n,selected) {
var list = document.getElementById(id);
$.post (‘ajax.php?act=getList’,{‘pid’:pid,’action’:id},function (data) {
var temp1 = eval(‘(‘+ data +’)’); //把傳過來的字元串轉化成一個JSON對象。
var leng = temp1.length;
var n = (n ids.length ) ? ids.length : n;
n = (n 0 ) ? 0 : n;
for (var j = n ; j ids.length ; j++)
{
var t = ‘temp’+j
t = document.getElementById(ids[j]);
t.options.length = 1;
t.options[0]=new Option(‘請選擇’,’*’);
}
if (leng 0) {
list.length = leng + 1;
for (var i=0;i temp1.length ;i++ )
{
list.options[i+1]=new Option(decodeURI(temp1[i].key),temp1[i].val);
if (temp1[i].region_id == selected ) {
list.options[0].selected = ‘selected’;
}
if (selectedlist.options[i+1].value==selected){
list.options[i+1].selected = ‘selected’;
}
}
}
if(pid == ‘*’) {
switch(id){
case ‘city’:
t = document.getElementById(‘city’);
t.options.length = 1;
t.options[0]=new Option(‘請選擇’,’*’);
t = document.getElementById(‘eare1’);
t.options.length = 1;
t.options[0]=new Option(‘請選擇’,’*’);
break;
case ‘eare1’:
t = document.getElementById(‘eare1’);
t.options.length = 1;
t.options[0]=new Option(‘請選擇’,’*’);
break;
}
}
if(document.getElementById(‘city’)document.getElementById(‘city’).value==’*’){
t = document.getElementById(‘eare1’);
t.options.length = 1;
t.options[0]=new Option(‘請選擇’,’*’);
}
});
}
$(function () {
getList (‘1′,’province’,1);
//三個都寫是為了修改的時候,請三個框中默認的都有選中的值,一般增加的時候只寫第一個就可以了。
});
/script
/head
body
div
select name=”yc1″ id=”province” onchange=”getList (this.value,’city’,1)”
option value=”*” selected=”selected”請選擇/option
/select
select name=”yc2″ id=”city” onchange=”getList (this.value,’eare1′,2)”
option value=”*” selected=”selected”請選擇/option
/select
select name=”yc3″ id=”eare1″
option value=”*” selected=”selected”請選擇/option
/select
/div
/body
/html
AJAX 頁面是
?php
$link = mysql_connect(“localhost”, “root”, “123456”);
mysql_select_db(“mydatabase”);
$act = isset ($_GET[‘act’]) ? $_GET[‘act’] : ” ;
$action = isset ($_POST[‘action’]) ? $_POST[‘action’] : ” ;
$pid = isset ($_POST[‘pid’]) ? $_POST[‘pid’] : ” ;
$arr = array();
switch ($action) {
case ‘province’:
$sql = “select DISTINCT(province_name) val,province_id key from province order by id”;
$res = mysql_query($sql);
while($col = mysql_fetch_array($res)){
$arr[] = $col;
}
break;
case ‘city’:
$sql = “select DISTINCT(city_name) val,city_id key from city where `province_id` = ‘”.$pid.”‘
order by id”;
$res = mysql_query($sql);
while($col = mysql_fetch_array($res)){
$arr[] = $col;
}
break;
case ‘eare1’:
$sql = “select DISTINCT(eare1_name) val,eare1_id key from eare1 where `city_id` = ‘”.$pid.”‘
order by id”;
$res = mysql_query($sql);
while($col = mysql_fetch_array($res)){
$arr[] = $col;
}
break;
}
mysql_close($link);
$list = array();
$i = 0;
foreach($arr as $k = $v){
foreach($v as $key = $value){
if(!preg_match(“|^\d+|”,$key)){
$list[$i][$key] = $value;
}
}
$i++;
}
print_r (json_encode ($list));
資料庫關聯是
表province 區域表
有ID province_id province_name三個欄位
id是自增的 province_id為唯一
表CITY 城市表
有ID city_id city_name province_id
其中province_id與province表的province_id對應
id是自增的 city_id為唯一
最後一個表類似與city表
jquery.js可以到下載
如果有問題可以在晚上7-10點 在H!給我留言
求php橫向二級聯動菜單
1.數據封裝成
[ {
id:”,
name:”,
submenu[
{id:”,name:”,link:”}
]
},
{…}
]
這種形式,submenu存的就是每個菜單的子菜單
2.對一級菜單加上mouseover 事件監聽
(1) 在監聽事件中獲取當前菜單的位置
(2)將子菜單的列在一個div上,並將div顯示一級菜單的右側(需要自己計算相對位置)
(3) 子菜單的div添加滑鼠移開事件,滑鼠移開時子菜單隱藏
這就是這種菜單的一種實現方式,至於代碼,還是你自己嘗試著去寫吧
3.如果嫌麻煩,網上也應該能找到類似的效果的源代碼,直接用
求php二級聯動下拉菜單
一般用ajax,比如:
大類的select onchange=”xxx javascript函數(參數)”
參數就是大類的id值
在xxx javascript函數(大類id)裡面
你用ajax把大類id傳給一個php文件,這個php文件從資料庫中取出這個大類下面的小類的值,再返回給當前的頁面,當前的頁面,再重新寫小類的slelct。
比如小類的select是這樣的:
div id=’xiaolei_div’
select id=”xiaolei_select”
xxxx
/select
/div
噹噹前頁面獲取到小類的值之後:
在js函數裡面:
var xiaolei_div=document.getElementById(‘xialei_div’);
var options=”;
for(xxxx)
{
options=options+’option value=”小類的id”小類的名稱/option’;
}
xiaolei_div.innerHtml=’select id=”xiaolei_select”‘+options+’/select’;
只是說說思路,代碼現打的,可能有錯誤
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/190320.html