萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> ajax讀取數據庫內容實現二級聯動下拉選擇菜單示例

ajax讀取數據庫內容實現二級聯動下拉選擇菜單示例

本文為大家介紹下使用ajax技術讀取數據庫內容並生成二級聯動下拉選擇菜單,具體實現如下,感興趣的朋友可以參考下,希望對大家有所幫助   復制代碼 代碼如下:


<PRE class=javascript name="code"></PRE><PRE class=javascript name="code">—————————————————————這是ajax(javascript)代碼 ———————————————————————————</PRE><PRE class=javascript name="code"></PRE><PRE class=javascript name="code">function send_request(callback, urladdress, isReturnData){
var xmlhttp = getXMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4) {//readystate 為4即數據傳輸結束
try{
if(xmlhttp.status == 200){
if(isReturnData && isReturnData==true){
callback(xmlhttp.responseText);
}
}else{
callback("抱歉,沒找到此頁面:"+ urladdress +"");
}
} catch(e){
callback("抱歉,發送請求失敗,請重試 " + e);
}
}
}
xmlhttp.open("POST", urladdress, true);
xmlhttp.send(null);
}
function getXMLHttpRequest() {
var xmlhttp;
if (window.XMLHttpRequest) {
try {
xmlhttp = new XMLHttpRequest();
xmlhttp.overrideMimeType("text/html;charset=UTF-8");//設定以UTF-8編碼識別數據
} catch (e) {}
} else if (window.ActiveXObject) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHttp");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Msxml3.XMLHttp");
} catch (e) {}
}
}
}
return xmlhttp;
}
</PRE>————————————————————————這是客戶端表單jsp代碼——————————————————————————————<BR>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type"
content="text/html; charset=UTF-8"><title>Insert title here</title><script type="text/javascript" src="<%=request.getContextPath()%>/js/xmlhttp.js"></script><script type="text/javascript">function getProcess(value) {var process = document.getElementById('process');send_request(function(value2)
{process.innerHTML = value2;}, "getProcessType.action?value="+value, true);}</script></head><body><div><form action="" method="post" name="form"><select name="process" onchange="getProcess(this.value)"><option value="0" selected>請選擇流程種類</option><option value="Y">業務流程</option><option
value="G">管理流程< /option><option value="Z">支持流程</option></select><div id="process"><select name="smallclass"><option value="一級流程名稱" selected>請選擇一級流程名稱</OPTION></select></div><input type="submit" value="提交"></form></div></body></html>
<PRE></PRE>
<BR>
<P><PRE class=html name="code"><PRE class=html name="code">————————————————————————這是服務端action代碼 ——————————————————————————————</PRE><BR>
<P></P>
<PRE></PRE>
這裡是我的業務邏輯,每個邏輯不同,所以這段代碼這只是為了把想要顯示的內容放在request范圍內,然後return到下一個頁面.javabean中有我的MyProcess類和它的屬性
<P></P>
<P><PRE class=java name="code">public String getProcessType(){
HttpServletRequest request=ServletActionContext.getRequest();
String value=request.getParameter("value");
List<MyProcess> MyProcesses= processService.getProcessByType(value);
for(MyProcess p:MyProcesses){
System.out.println(p.getName());
}
request.setAttribute("list",MyProcesses);
return SUCCESS;
}</PRE><PRE class=html name="code"></PRE><PRE class=html name="code"></PRE><PRE class=html name="code">————————————————————————這是服務端jsp代碼 ——————————————————————————————</PRE><PRE class=html name="code"><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/page/share/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<select name="smallclass">
<option value="一級流程名稱" selected>請選擇一級流程名稱</OPTION>
<c:forEach items="${list}" var="myprocess">
<option value="${myprocess.processID}" >
${myprocess.name}
</option>
</c:forEach>
</select>
</body>
</html></PRE><BR>
<BR>
<P></P>
<P>這個過程差不多就這些!</P>
<P><BR>
</P>
</PRE>

copyright © 萬盛學電腦網 all rights reserved