一个动态生成菜单的JS.保存以备后用.

来源:互联网 发布:阳台封窗价格计算法 编辑:程序博客网 时间:2024/06/05 01:53

<!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" dir="ltr">
<head profile="http://gmpg.org/xfn/11">
</head>
<script type="text/javascript" >
SelId = 1 ;

var menuId = new Array(0,1,2,3);
var menuName = new Array(0,1,2,3);

menuId[0]   = new Array(-1,201,307,402);
menuId[1]   = new Array(731,502,984);
menuId[2]   = new Array(718,105,121);
menuId[3]   = [111];

menuName[0] = new Array("全部","美剧","故事剧","日剧");
menuName[1] = new Array("越狱","大陆剧","韩剧");
menuName[2] = new Array("台剧","情景剧","喜剧");
menuName[3] = ["喜剧"];

function addOpt(id,curSel)
{
 if(id >= 4)
  return 0;
 if( ( parseInt( curSel.substr( 6 )) + 1 ) != id ) {
  return 0 ;
 }
 var obj = document.getElementById( "select" + id ) ;
 if ( obj || 'undefined' == obj ) {
  return 0 ;
 }
 
 if(document.getElementById( "select0" ).value == 0){
  for(var j=1;j<=4;j++){
   document.getElementById("d").removeChild(document.getElementById("select"+j));   
  }
 }
 
 var addSel=document.createElement("select");

 addSel.id="select"+id ;
 addSel.name="cs[]";

 var ooption=new Array();

 for ( var i=0;i<menuId[id].length;i++){
  addSel.options[i]=new Option(menuName[id][i],menuId[id][i]);
 }
 document.getElementById("d").appendChild(addSel);
 document.getElementById("d").removeChild(document.getElementById("select0"));
 SelId++ ;
 addSel.onchange = function(){addOpt(SelId,"select"+id)};
}
</script>
<body>
<form action="" method="post">
<span id="d">
<select name="cs[]" id="select0" onchange="addOpt(SelId,this.id);">
<option value="-1">全部</option>
<option value="201">美剧</option>
<option value="307">故事剧</option>
<option value="402">日剧</option>
</select>
</span>
<br />
<input type="submit" value="submit"/>
</form>
<pre />
<?php var_dump($_REQUEST)?>
</body>
</html>