ajax返回集合取值问题....

houkai1987 2011-11-04 09:46:21
function getList(list){

}
list里面查询返回ID,MC的集合 怎么取出来 添加到下拉框啊!
...全文
188 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangpeng9886 2011-11-18
  • 打赏
  • 举报
回复
ajax返回对象数组直接取是取不出来的,可以通过json取出来
sasgsc 2011-11-18
  • 打赏
  • 举报
回复
不能理解你想要干什么.
参考:
$.getJSON("test.js", { name: "John", time: "2pm" }, function(json){
alert("JSON Data: " + json.users[3].name);
});
这是异步的.

$.each( { name: "John", lang: "JS" }, function(i, n){
alert( "Name: " + i + ", Value: " + n );
});
这是你说的数组遍历.
chabale 2011-11-18
  • 打赏
  • 举报
回复
1.通过json对象来传递
2.在服务器端对集合进行处理,将集合中的内容用","隔开,连接成一个字符串,然后再传到js,js通过splite(","),解析出来
你是不是在做 省市联动
ajax省市联动
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'province.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function loadXMLDoc()
{
xmlhttp = null;
var country = document.getElementById("country");
country.length = 1;
country.selectedIndex = 0;
var province = document.getElementById("province");
if(province.value == ""){
return ;
}
var url="http://localhost:8080/Ajax_Province/GetProvince?&province="+encodeURIComponent(province.value);
if (window.XMLHttpRequest) {// code for Firefox, Mozilla, IE7, etc.
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp != null) {
xmlhttp.onreadystatechange = state_Change;
xmlhttp.open("post", url, true);
xmlhttp.send(null);
} else {
alert("Your browser does not support XMLHTTP.");
}
}

function state_Change() {
var province = document.getElementById("province");
var country = document.getElementById("country");
if (xmlhttp.readyState == 4&&xmlhttp.status == 200) {// 4 = "loaded"
var serviceData = xmlhttp.responseText;
if(serviceData == null||serviceData == ""){
return;
}
var s = serviceData.split(",");
for(var i=0;i<s.length-1;i++){
country.options[i+1] = new Option(s[i],s[i]);
}
}
}




</script>
</head>

<body>
出生地:<select id="province" onchange="loadXMLDoc()">
<option value="">------请选择省-----</option>
<option value="江西省">江西省</option>
<option value="江苏省">江苏省</option>
<option value="浙江省">浙江省</option>
<option value="山东省">山东省</option>
<option value="辽宁省">辽宁省</option>
<option value="福建省">福建省</option>

</select>

<select id="country"><option value="">-----请选择市-----</option></select>
</body>
</html>
servlet:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neusoft.zjl.service.ProvinceService;


public class GetProvince extends HttpServlet {

/**
* Constructor of the object.
*/
public GetProvince() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List list = null;
ProvinceService provinceService = new ProvinceService();
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String strprovince = request.getParameter("province");
if(strprovince == null||"".equals(strprovince)){
return ;
}
String province = new String(strprovince.getBytes("ISO-8859-1"),"UTF-8");
list = provinceService.getCountry(province);//这个是查询数据库的你可以换你的对数据库的操作
if(list ==null){
return;
}
StringBuffer sb = new StringBuffer();
for(int i=0;i<list.size();i++){
String s = (String)list.get(i);
sb.append(s+",");
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print(sb);
out.flush();
out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}
yangsen251024 2011-11-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sophie317 的回复:]
用json对象
[/Quote]
一般遇到这种情况都用json对象
敬敬11 2011-11-18
  • 打赏
  • 举报
回复
后台用
Struts2Utils.renderJson(list);把list转换为json
前台
$.ajax( {
// 请求地址
url : '${ctx}/product/productAction!showLocation.action?id='+id,
method:'POST',
async : false,
// 成功时回调
success : function(response, options) {
var result = Ext.util.JSON.decode(response.responseText);
var selectLst = document.getElementById(select_id) ;
selectLst.options.length = 0;
var oOption = document.createElement("OPTION");
selectLst.options.add(oOption);
oOption.value='';
oOption.innerText ='-请选择-';
for(key in result){
var oOption = document.createElement("OPTION");
selectLst.options.add(oOption);
oOption.value=key;
oOption.innerText =result[key];
}
}

});
照着上面的格式
Struts2Utils需要springside3-core-3.3.3.jar 你下载一个
sophie317 2011-11-15
  • 打赏
  • 举报
回复
用json对象
Geminit 2011-11-15
  • 打赏
  • 举报
回复
用json
struts2中后台action中有List变量时,添加get方法
前台页面js $('list') 可取到
试试看 希望有帮助
yujinjin9 2011-11-04
  • 打赏
  • 举报
回复
这什么乱七八糟的呀!是JS还是JAVA呀
如果是JS,可没有LIST这一集合概念,只有数组
CloudX2019 2011-11-04
  • 打赏
  • 举报
回复
function getList(list){
for(var e in list){
alert(e+"\t"+list[e]);
}
}


你会有惊喜

67,514

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧