js级联

var type=
{
  首页导航:["首页导航"],
  首页活动:[ '有奖问答','用户调查','直播间']
};
var searchModuleVaule=
{
  首页导航:["0"],
  首页活动:[ '1','2','3']
};
//加载类型
function loadType()
{
  //获取类型所对应的列表框对象
  var pro=document.getElementById("searchType");
  //从数组中提取类型份信息
  for(var p in type)
  {
   //创建option元素
   var opt=document.createElement("option");
   //设置option元素中的信息
   opt.innerText=p; 123
   opt.value=p;
   if(p=='${searchTypeRequest}')
   {
   opt.selected =true;
   }
   //将option元素添加到select元素中(option是select的子节点)
   pro.appendChild(opt);
  }
  if(${searchTypeRequest!=null or searchTypeRequest!=''})
  {
  loadSearchModule('${searchTypeRequest}') ;
  }
}

//自动加载模块
function loadSearchModule(selectType)
{
  //根据类型获取模块(searchModules是一个数组)
  var searchModules=type[selectType];
  var searchModulesValue=searchModuleVaule[selectType];
  //获取模块所对应的列表框对象
  var searchModule=document.getElementById("searchModule");
  //将元素列表框中的元素全部清空
  searchModule.innerText="";
 
  var opt=document.createElement("option");
  opt.innerText="全部";
  opt.value="";
  searchModule.appendChild(opt);
  for(var index=0;index<searchModules.length;index++)
  {
   opt=document.createElement("option");
   opt.innerText=searchModules[index];
   opt.value=searchModulesValue[index];
   if(opt.value=='${searchModule}')
   {
   opt.selected =true;
   }
   searchModule.appendChild(opt);
  }
}
//测试用
function test()
{
var selectCity=document.getElementById("searchModule").value;
alert(selectCity);
}
//在窗口加载时添加类型信息
window.onload=loadType;

你可能感兴趣的:(活动)