新聞中心
菜鳥求助,調(diào)用數(shù)據(jù)庫實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)問題js java?
后臺(tái)能夠打印出來,說明沒有問題。問題應(yīng)在前邊的接收端??创a返回的是json string,對(duì)于前端來說應(yīng)該僅僅是個(gè)string,而不是JS對(duì)象。
創(chuàng)新互聯(lián)2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目做網(wǎng)站、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元秀山土家族苗族做網(wǎng)站,已為上家服務(wù),為秀山土家族苗族各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
記得JQuery,發(fā)送request的時(shí)候有參數(shù),可以自動(dòng)把返回的json string變成js的object的。
JAVA實(shí)現(xiàn)Ajax三級(jí)聯(lián)動(dòng)?
那就給你說下二級(jí)聯(lián)動(dòng)吧 ,三級(jí)道理也是一樣的。
根據(jù)第一級(jí)下拉框,點(diǎn)擊事件后,將id傳遞給服務(wù)器,服務(wù)器根據(jù)該id,經(jīng)過數(shù)據(jù)庫查詢,獲得二級(jí)下拉框的內(nèi)容,這里的內(nèi)容你可以用xml封裝,或者是直接寫成dom格式:如dom格式
服務(wù)端:
PrintWriter writer = response.getWriter();
writer.println("select");
while(rs.next()){
writer.println("option"+rs.getString("xxx")+"/option");
writer.flush();
}
writer.println("/select");
writer.close();
客戶端:
var selectStr=xmlHttp.responseText;//二集菜單的內(nèi)容
找個(gè)元素,將二級(jí)菜單項(xiàng)利用innerHTML插入到網(wǎng)頁中
document.getElementById("xx").innerHTML=selectStr;
不知道這樣說你明白不?
實(shí)現(xiàn)第三級(jí),只需要在第二個(gè)下拉框里觸發(fā)事件后,將id傳遞給服務(wù)器,在經(jīng)過處理,將結(jié)果給第三級(jí)下拉框
三級(jí)聯(lián)動(dòng)怎么回顯 javaweb
以省,市,城聯(lián)動(dòng)為例:
此小程序的功能主要是采用異步請(qǐng)求方式從數(shù)據(jù)庫中調(diào)取省市區(qū)信息顯示到下拉列表:
代碼如下:
建立數(shù)據(jù)庫中的代碼和一些配置文件信息就省略了,主要有JavaScript中的代碼為:
$(document).ready(function(){
$.get("getProvince.do", function(result){
$("#showp").html(result);
});
})
var xmlhttp;
function mysend(str){
$(document).ready(function(){
$("#show2").html("");
})
var show = document.getElementByIdx_x_x_x_x_x_x_x_x_x("show");
show.innerHTML = "";
var province = document.getElementByIdx_x_x_x_x_x_x_x_x_x("province").value;
if(province!=0){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 xmlhttp.status==200){
show.innerHTML = xmlhttp.responseText;
}
}
var ss = encodeURIComponent(str);
xmlhttp.open("GET","getCity.do?provinceid="+ss,true);
xmlhttp.send(null);
}
}
function myarea(str){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 xmlhttp.status==200){
var show2 = document.getElementByIdx_x_x_x_x_x_x_x_x_x("show2");
show2.innerHTML = xmlhttp.responseText;
}
}
var ss = encodeURIComponent(str);
xmlhttp.open("GET","getArea.do?cityid="+ss,true);
xmlhttp.send(null);
}
html頁面中的代碼為:
所在地
action中的代碼為:
package mobi.zhangsheng.jiejia.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import mobi.zhangsheng.jiejia.domain.Areas;
import mobi.zhangsheng.jiejia.service.AgentsService;
import mobi.zhangsheng.jiejia.service.AreasService;
@Controller
public class ProvinceAction {
private int provinceid;
private int cityid;
@Resource
private AreasService as;
@Resource
private AgentsService ags;
public int getProvinceid() {
return provinceid;
}
public void setProvinceid(int provinceid) {
this.provinceid = provinceid;
}
public int getCityid() {
return cityid;
}
public void setCityid(int cityid) {
this.cityid = cityid;
}
public void getProvince(){
List provinceList = as.getAreasPrvinceList();
HttpServletResponse resp= ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
//resp.setContentType("xml");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
try {
PrintWriter out = resp.getWriter();
out.print("");
//out.print("shanghai");
} catch (IOException e) {
e.printStackTrace();
}
}
public void getCity(){
List cityList = as.getAreasCityList(provinceid);
HttpServletResponse resp= ServletActionContext.getResponse();
//resp.setContentType("xml");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
try {
PrintWriter out = resp.getWriter();
out.print("");
//out.print("shanghai");
} catch (IOException e) {
e.printStackTrace();
}
}
public void getArea(){
List areaList = as.getAreasCityList(cityid);
if(areaList.size()==0){
}else{
HttpServletResponse resp= ServletActionContext.getResponse();
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
try {
PrintWriter out = resp.getWriter();
out.print("");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
本文題目:java中三級(jí)聯(lián)動(dòng)的代碼 java中三級(jí)聯(lián)動(dòng)的代碼有哪些
轉(zhuǎn)載來源:http://m.biofuelwatch.net/article/doggehd.html