js+php+DB 下拉菜单两级联动

 
<html>

<head>
<script language="javaScript">
  var equiptList = new Array();  //定义一个全局数组变量
  function LoadEquip()
  {
     document.myform.selEquit.options.length=1; //设置设备的下拉条只有1项
  <?php
     require ("config.inc");
     $sql = "select distinct(equipment_name)  from  Equipment_info ";
     $eqNames = mssql_query($sql,$conn);
 
     while($eqs = mssql_fetch_array($eqNames))
     {
         echo "equiptList['$eqs[equipment_name]']=[];";   //每个循环生成一个空数组,比如 equiptList['电脑']=[]
         echo "document.myform.selEquit.options.add(new Option(\"$eqs[equipment_name]\",\"$eqs[equipment_name]\"));"; //先在设备下拉框里面放好Option
             /// 好的, 现在就要通过获得的equipment_Name去获取相应的type
             $getTypeSql = "select distinct(Equipment_Type)  from dbo.Equipment_info where Equipment_Name = '$eqs[equipment_name]' ";
             $Types = mssql_query($getTypeSql);
             //下面这个while 填充数组元素,如 equiptList['电脑']=['aa','bb','cc','dd']的形式
             while($tps = mssql_fetch_array($Types))
             {
                 echo "equiptList['$eqs[equipment_name]'].push('$tps[Equipment_Type]');"; //将元素push进数组里面去
             }
     }    
  ?>
  }
  
  function updateType()
  {
     var pIndex = document.myform.selEquit.value;   //获得设备的Value值
     var newOptionType;                             //new一个Option给设备型号
     document.myform.selType.options.length=0;      
 
     for(var j in equiptList[pIndex])               //从数组里面循环取元素出来
     {
         newOptionType = new Option(equiptList[pIndex][j],equiptList[pIndex][j]);    //每个元素new给Option
         document.myform.selType.options.add(newOptionType);                         //添加进select控件
     }
  }
</script>
</head>

<body οnlοad="LoadEquip()">
      
<form action="" name="myform" >

<select name="selEquit" οnchange="updateType()" >  <!--οnchange="updateType()" -->
  <option> -- 选择设备 -- </option>

  </select>

<select name="selType">
  <option> --选择型号-- </option>
</select>
</form>
</body>
</html>

数据表简单:

 

CREATE TABLE [dbo].[Equipment_info](
	[UID] [int] IDENTITY(1,1) NOT NULL,
	[Equipment_Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[Equipment_Type] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NULL,
) ON [PRIMARY]


 


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值