php二级联动菜单
这个是我的代码,我想知道如何能获取两个下拉菜单的value?具体是哪几句代码负责定义每个选项的value是多少?<metahttp-equiv="Content-Type...
这个是我的代码,我想知道如何能获取两个下拉菜单的value?具体是哪几句代码负责定义每个选项的value是多少?
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>联动菜单</title>
<body>
<SCRIPT>
function Dsy()
{
this.Items = {};
}
Dsy.prototype.add = function(id,iArray)
{
this.Items[id] = iArray;
}
Dsy.prototype.Exists = function(id)
{
if(typeof(this.Items[id]) == "undefined") return false;
return true;
}
function change(v){
var str="0";
for(i=0;i<v;i++){ str+=("_"+(document.getElementById(s[i]).selectedIndex-1));};
var ss=document.getElementById(s[v]);
with(ss){
length = 0;
options[0]=new Option(opt0[v],opt0[v]);
if(v && document.getElementById(s[v-1]).selectedIndex>0 || !v)
{
if(dsy.Exists(str)){
ar = dsy.Items[str];
for(i=0;i<ar.length;i++)options[length]=new Option(ar[i],ar[i]);
if(v)options[1].selected = true;
}
}
if(++v<s.length){change(v);}
}
}
var dsy = new Dsy();
dsy.add("0",[
<?php
$dbhost="localhost";
$dbname="modoer";
$dbc_name="root";
$dbpass="";
$conn=mysql_connect($dbhost,$dbc_name,$dbpass) or die("Could not connect to MySQL server!");
mysql_select_db($dbname) or die("Could not select database!");
mysql_query('set names "utf8"');
$sql="select * from modoer_service_type where servpid=0";
$result=mysql_query($sql);
$count=mysql_affected_rows();
$i=0;
while($val=mysql_fetch_array($result))
{
$val1[$i]=$val;
echo "\"".$val['servname']."\",";
$i++;
}
echo "\"其他\"";
?>]);
<?php
for($i=0;$i<$count;$i++)
{
echo "dsy.add(\"0_".$i."\",[";
$sql2="select * from modoer_service_type where servpid=".$val1[$i]['servid'];
$result=mysql_query($sql2);
while($val=mysql_fetch_array($result))
{
echo "\"".$val['servname']."\",";
}
echo "\"其他\"]);\n";
}
echo "dsy.add(\"0_".$i."\",[";
echo "\"其他\"]);\n";
?>
</SCRIPT>
<SCRIPT>
var s=["s1","s2"];
var opt0 = ["请选择","请选择"];
function setup()
{
for(i=0;i<s.length-1;i++)
document.getElementById(s[i]).onchange=new Function("change("+(i+1)+")");
change(0);
}
</SCRIPT>
<select name="lz_sf" id="s1">
<option></option></select>
<select name="lz_sx" id="s2">
<option></option></select>
<SCRIPT language="javascript">
setup()
</SCRIPT> 展开
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>联动菜单</title>
<body>
<SCRIPT>
function Dsy()
{
this.Items = {};
}
Dsy.prototype.add = function(id,iArray)
{
this.Items[id] = iArray;
}
Dsy.prototype.Exists = function(id)
{
if(typeof(this.Items[id]) == "undefined") return false;
return true;
}
function change(v){
var str="0";
for(i=0;i<v;i++){ str+=("_"+(document.getElementById(s[i]).selectedIndex-1));};
var ss=document.getElementById(s[v]);
with(ss){
length = 0;
options[0]=new Option(opt0[v],opt0[v]);
if(v && document.getElementById(s[v-1]).selectedIndex>0 || !v)
{
if(dsy.Exists(str)){
ar = dsy.Items[str];
for(i=0;i<ar.length;i++)options[length]=new Option(ar[i],ar[i]);
if(v)options[1].selected = true;
}
}
if(++v<s.length){change(v);}
}
}
var dsy = new Dsy();
dsy.add("0",[
<?php
$dbhost="localhost";
$dbname="modoer";
$dbc_name="root";
$dbpass="";
$conn=mysql_connect($dbhost,$dbc_name,$dbpass) or die("Could not connect to MySQL server!");
mysql_select_db($dbname) or die("Could not select database!");
mysql_query('set names "utf8"');
$sql="select * from modoer_service_type where servpid=0";
$result=mysql_query($sql);
$count=mysql_affected_rows();
$i=0;
while($val=mysql_fetch_array($result))
{
$val1[$i]=$val;
echo "\"".$val['servname']."\",";
$i++;
}
echo "\"其他\"";
?>]);
<?php
for($i=0;$i<$count;$i++)
{
echo "dsy.add(\"0_".$i."\",[";
$sql2="select * from modoer_service_type where servpid=".$val1[$i]['servid'];
$result=mysql_query($sql2);
while($val=mysql_fetch_array($result))
{
echo "\"".$val['servname']."\",";
}
echo "\"其他\"]);\n";
}
echo "dsy.add(\"0_".$i."\",[";
echo "\"其他\"]);\n";
?>
</SCRIPT>
<SCRIPT>
var s=["s1","s2"];
var opt0 = ["请选择","请选择"];
function setup()
{
for(i=0;i<s.length-1;i++)
document.getElementById(s[i]).onchange=new Function("change("+(i+1)+")");
change(0);
}
</SCRIPT>
<select name="lz_sf" id="s1">
<option></option></select>
<select name="lz_sx" id="s2">
<option></option></select>
<SCRIPT language="javascript">
setup()
</SCRIPT> 展开
1个回答
展开全部
我给你提供一个三级的
主页面是
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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 (selected&&list.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可以到http://jquery.com/下载
如果有问题可以在晚上7-10点 在H!给我留言
主页面是
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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 (selected&&list.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可以到http://jquery.com/下载
如果有问题可以在晚上7-10点 在H!给我留言
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询