用jquery实现下拉菜单联动[调用数据库]

<script type="text/javascript">
//显示联动菜单
//初始化
        $.ajax({
            url: "showIllegalSelectOption",//调用方法
			type: "GET",
		    data: {id:"0"},//参数
		    dataType: "json",//类型

			success: function(data) {
			  var tbHtml = "";//定义HTML
			  tbHtml += "<option></option>";
			  $.each(data, function(key, value) {//循环
				  tbHtml+="<option value="+value.id+">"+value.name+"</option>"//显示查询出来的结果内容
			  });
			  
			  $('#ddlCompany').html(tbHtml);//显示HTML
		  }
		  
           });

       $(document).ready(function() {


            $('#ddlCompany').change(function() {//变更事件

             GetData();//获取第二个select数据

            });
           });

 

           function GetData() {
           var id = $('#ddlCompany').val();
           $.ajax({
            url: "showIllegalSelectOption",
			type: "GET",
		    data: {id : $("#ddlCompany").val()},
		    dataType: "json",
            success: function(data) {
			 $("#iid").empty();//清空
			  $("#iid").show();//显示select
				 var secondHtml = "";
				 secondHtml += "<option></option>";
			  $.each(data, function(key, value) {
				  secondHtml+="<option value="+value.id+">"+value.name+"</option>"
			  });
            
             $("#iid").html(secondHtml);
            }
           });
           }
</script> 


html页面:两个select,第一个默认显示,第二个默认隐藏

<div ID="ddlSummary">
     <select id="ddlCompany">

     </select>
      <select id="iid" style="display:none">

     </select>
  </div>

java

	//显示illegal表中的下拉菜单,通过调用数据库返回LIST
	@RequestMapping(value="/showIllegalSelectOption", method=RequestMethod.GET)
	public @ResponseBody Object showIllegalSelectOption(HttpServletRequest request,HttpServletResponse response) {
 
		String id = request.getParameter("id");
		//Object... obj 
		List<Object> paraes = new ArrayList();
		String hql = "from Illegal wc where 1=1 ";

		if(id != null && !id.equals(""))
		{
			hql += "and supername=? ";
			paraes.add(id);
		}	
		
		List<Illegal> queryResult=null;
		
		if(paraes.size() <= 0)
		{
			queryResult=illegalService.queryIllegal(hql);
		}
		else if(paraes.size() == 1)
		{
			queryResult=illegalService.queryIllegal(hql, paraes.get(0));
		}
		else
		{
			queryResult=illegalService.queryIllegal(hql, paraes.get(0), paraes.get(1));
		}
		
		return queryResult;
	}


ASP 加数据库 四级菜单联动 var m_oXMLDoc = new ActiveXObject("Microsoft.XMLDOM"); var m_sBaseSrc = "Tree.asp?ParentCode="; //源码爱好者(http://www.codefans.net) function BindSelect( strXMLSrc , objSelectName, defaultVal) { m_oXMLDoc.async = true; m_oXMLDoc.onreadystatechange = Function( "fnLoadComplete('" + objSelectName + "', '" + defaultVal + "');" ); m_oXMLDoc.load( strXMLSrc ); } function fnLoadComplete(objSelectName, defaultVal) { var objSelect = document.all[objSelectName]; var aryXMLNodes; var node; if (objSelect == null) return; try { var iReadyState = m_oXMLDoc.readyState; } catch(e) { return; } if( iReadyState != 4 ) return; if( m_oXMLDoc != null && m_oXMLDoc.xml != "" ) { objSelect.length = 0; aryXMLNodes = m_oXMLDoc.documentElement.selectNodes("TreeNode"); objSelect.options[0] = new Option("==全部=="); for (var i=0; i 1) { SetSelectedValue(objSelect, defaultVal) } if(objSelect.ChildSelectName != null) { objSelect.onchange = Function( "var val = this.options[this.selectedIndex].value;BindSelect(m_sBaseSrc+val, '"+objSelect.ChildSelectName+"', '"+defaultVal+"');if(val!='')document.all.MaterialClassCode.value=val;" ); objSelect.fireEvent("onchange"); } else { objSelect.onchange = Function( "var val = this.options[this.selectedIndex].value;if(val!='')document.all.MaterialClassCode.value=val;" ); } } } function InitSelect(defaultVal) { document.all.MaterialClassCode.value = ""; BindSelect( m_sBaseSrc + "00", "MaterialClass1", defaultVal); } function Equality(val1,val2) { if (val1.length < val2.length || val2 == "") return false; return (val1.substr(0,val2.length) == val2) } function SetSelectedValue(oSel,val) { if (val == null) return; for(var i=0; i<oSel.length; i++) { if (Equality(val, oSel.options[i].value)) { oSel.selectedIndex = i; if(oSel.ChildSelectName == null) oSel.fireEvent("onchange"); break; } } } window.onload = function(){InitSelect();}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值