利用了jquery的ajax實現(xiàn)二級聯(lián)互動菜單
更新時間:2013年12月02日 17:26:56 作者:
二級聯(lián)互動菜單,利用了jquery的ajax實現(xiàn),具體實現(xiàn)如下,喜歡的朋友可以參考下
菜單資源保存在數(shù)據(jù)庫中。利用了jquery的ajax實現(xiàn)。用到的包有:json-lib-2.2.3-jdk15.jar ezmorph-1.0.6.jar json.js jquery.js
jsp頁面的代碼:
<%@ page contentType="text/html; charset=gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/json.js"></script>
<% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
out.println(basePath);
%>
<script type="text/javascript">
jQuery(function($){
//alert("ok");
});
function onchangeShow(oneId){
$.ajax({
url : "<%=basePath%>cateJson.whbs",
data : {parentId : oneId}, // 參數(shù)
type : "post",
cache : false,
dataType : "json", //返回json數(shù)據(jù)
error: function(){
alert('error');
},
success:onchangecallback
});
}
function onchangecallback(data){
document.all['twoId'].options.length = 0; //清空原有的option
var str="";
for(var i=0;i<data.length;i++){
str+="<option value='"+data[i].recordId+"'>"+data[i].title+"</option>"
}
$("#twoId").html(str);
}
</script>
<html>
<body>
<div align="center">
請選擇部門類型
<s:select list="rfones" listKey="recordId" listValue="title" name="oneId" theme="simple" id="oneId" value="oneID" onchange="onchangeShow(this.value)"></s:select>
請選擇文件類型
<s:select list="rftwos" listKey="recordId" listValue="title" name="twoId" theme="simple" id="twoId" value="twoID"></s:select>
</div>
</body>
</html>
struts中action的代碼
/**
* des:取得二級聯(lián)動菜單
* autho:exceljava
* date:Nov 20, 2009
* @return
* @throws IOException
*/
public String getJsonCategory() throws IOException{
rfjsons=archiveService.getCategoryByParentID(parentId);//這里從數(shù)據(jù)庫取得數(shù)據(jù)
net.sf.json.JSONArray jsonObj=net.sf.json.JSONArray.fromObject(rfjsons);//組裝成json數(shù)據(jù)
sendMessage(jsonObj.toString());//向視圖push json數(shù)據(jù)
return null;
}
/**
* des:封裝發(fā)送json格式的數(shù)據(jù)回js
* autho:exceljava
* date:Nov 20, 2009
* @param content
* @throws IOException
*/
public void sendMessage(String content) throws IOException{
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
response.getWriter().write(content);
}
jsp頁面的代碼:
復(fù)制代碼 代碼如下:
<%@ page contentType="text/html; charset=gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/json.js"></script>
<% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
out.println(basePath);
%>
<script type="text/javascript">
jQuery(function($){
//alert("ok");
});
function onchangeShow(oneId){
$.ajax({
url : "<%=basePath%>cateJson.whbs",
data : {parentId : oneId}, // 參數(shù)
type : "post",
cache : false,
dataType : "json", //返回json數(shù)據(jù)
error: function(){
alert('error');
},
success:onchangecallback
});
}
function onchangecallback(data){
document.all['twoId'].options.length = 0; //清空原有的option
var str="";
for(var i=0;i<data.length;i++){
str+="<option value='"+data[i].recordId+"'>"+data[i].title+"</option>"
}
$("#twoId").html(str);
}
</script>
<html>
<body>
<div align="center">
請選擇部門類型
<s:select list="rfones" listKey="recordId" listValue="title" name="oneId" theme="simple" id="oneId" value="oneID" onchange="onchangeShow(this.value)"></s:select>
請選擇文件類型
<s:select list="rftwos" listKey="recordId" listValue="title" name="twoId" theme="simple" id="twoId" value="twoID"></s:select>
</div>
</body>
</html>
struts中action的代碼
復(fù)制代碼 代碼如下:
/**
* des:取得二級聯(lián)動菜單
* autho:exceljava
* date:Nov 20, 2009
* @return
* @throws IOException
*/
public String getJsonCategory() throws IOException{
rfjsons=archiveService.getCategoryByParentID(parentId);//這里從數(shù)據(jù)庫取得數(shù)據(jù)
net.sf.json.JSONArray jsonObj=net.sf.json.JSONArray.fromObject(rfjsons);//組裝成json數(shù)據(jù)
sendMessage(jsonObj.toString());//向視圖push json數(shù)據(jù)
return null;
}
/**
* des:封裝發(fā)送json格式的數(shù)據(jù)回js
* autho:exceljava
* date:Nov 20, 2009
* @param content
* @throws IOException
*/
public void sendMessage(String content) throws IOException{
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
response.getWriter().write(content);
}
相關(guān)文章
js實現(xiàn)封裝jQuery的簡單方法與鏈式操作詳解
這篇文章主要給大家介紹了關(guān)于js實現(xiàn)封裝jQuery的簡單方法與鏈式操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2021-03-03js實現(xiàn)仿網(wǎng)易點擊彈出提示同時背景變暗效果
這篇文章主要介紹了js實現(xiàn)仿網(wǎng)易點擊彈出提示同時背景變暗效果,涉及javascript彈出框及頁面元素樣式操作的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-08-08微信小程序webview與h5通過postMessage實現(xiàn)實時通訊的實現(xiàn)
這篇文章主要介紹了微信小程序webview與h5通過postMessage實現(xiàn)實時通訊的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2019-08-08javascript實現(xiàn)網(wǎng)頁中涉及的簡易運動(改變寬高、透明度、位置)
這篇文章主要介紹了javascript實現(xiàn)網(wǎng)頁中涉及的簡易運動,比如改變寬高、透明度、位置等,感興趣的小伙伴們可以參考一下2015-11-11JavaScript實現(xiàn)的級聯(lián)算法示例【省市二級聯(lián)動功能】
這篇文章主要介紹了JavaScript實現(xiàn)的級聯(lián)算法,結(jié)合省市二級聯(lián)動下拉菜單功能實例分析了javascript事件響應(yīng)與元素動態(tài)操作實現(xiàn)級聯(lián)算法的相關(guān)技巧,需要的朋友可以參考下2018-12-12javascript firefox 自動加載iframe 自動調(diào)整高寬示例
iframe 自動獲取onload高寬以及iframe 自動加載,具體實現(xiàn)如下,感興趣的朋友可以參考下2013-08-08