通过JSON数组对象检索多个值
retrieve more than one value by JSON array objects
我通过JSON数组获得一个值,但如何在JSON数组中存储多个值,以及如何通过JavaScript检索它?
auto.jsp:
<script type="text/javascript">
$(document).ready(function() {
$("#combo").change(function() { // after onchange event it goes to combo1.jsp
$.getJSON('combo1.jsp', {
count: this.value
}, function(responseData) {
var splitValues = responseData.name.split(/,/);
$("#combo1").empty().append("<option>please select</option>");
for (var idx in splitValues) {
$("#combo1").append(
$("<option></option>").html(splitValues[idx]).val(splitValues[idx]));
}
});
});
});
</script>
<body>
//first combo box
<select id="combo" name="count">
<option value="">please select</option>
<option value="a">A</option>
</select>
//second combo box
<select id="combo1" name="combo1Val">
// i am getting only "5" here, but i want to show 1,2,3,4,5 as in drop down list
</select>
</body>
combo1.jsp:
<%
String count=request.getParameter("count");// by onchange event of first combo, i am
getting value "a" here
if(count.equalsIgnoreCase("a")){
// in my db there are 5 values i.e. 1,2,3,4,5 for single value "a", but while
populating in second combo i am getting only one value "5", how? please ignore my db
connection in jsp
JSONObject arrayObj= new JSONObject();
// by db connection i am fetching 5 values but while printing in javascript i am
getting only last one that is "5" in second combo, how can i populate all values
1,2,3,4,5 as drop down items in second combo box?
// retrieveing 5 datas(1,2,3,4,5) from db where name =a
while(rs.next()){
t1=(String)(rs.getString(1));// there are 5 values in db relating to "a", but i
am getting only last value i.e. "5" in second combo
}
arrayObj.put("name",t1);
response.setContentType("application/json");
response.getWriter().write(arrayObj.toString());
}
%>
我认为你在第二个组合中只得到了5,因为你在循环中犯了一个错误。你可以做
//in combo1.jsp
String selectedValue = request.getParameter("count");
Map<String, String> options = new Map<String, String>();
//get your data from db
while(rs.next()){
String t1=(String)(rs.getString(1));
options.add(t1, t1);
}
String json = new Gson().toJson(options);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
//to get data
$.getJSON('combo1.jsp', {
count: this.value
}, function(options) {
var dropdown2 = $('#combo1');
$('>option', dropdown2).remove(); // Clean old options first.
if (options) {
$.each(opts, function(key, value) {
dropdown2.append($('<option/>').val(key).text(value));
});
} else {
dropdown2.append($('<option/>').text("Please select dropdown1"));
}
});
相关文章:
- 如何从类关系中检索对象列表
- 检索对象项的路径
- 正在根据ID从数组中检索对象
- 以文本形式检索对象元素中的SVG Dom
- AJAX来检索对象并将其存储在变量中
- 如何在帮助程序或事件中检索对象的内部_id
- 如何在 ydn-db 中检索对象的排序列表
- Javascript - 检索对象属性路径
- 从具有特定参数的数组中检索对象
- 如何从jsonString中检索对象
- 检索对象自己的标记
- 从URL检索对象/资源
- 如何从Google Apps脚本项目属性中存储和检索对象
- 如何从GTM数据层中检索对象
- JQueryAJAX检索对象数组
- 正在检索对象中嵌套的值
- 使用PHP从JSON中检索对象
- 如何从具有给定(键、值)对的长列表中检索对象
- 使用承诺从 mongo db 检索对象时出错
- 在树状的angularjs模型中检索对象