Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过JSON数组对象检索多个值_Javascript_Jquery_Ajax_Json_Jsp - Fatal编程技术网

Javascript 通过JSON数组对象检索多个值

Javascript 通过JSON数组对象检索多个值,javascript,jquery,ajax,json,jsp,Javascript,Jquery,Ajax,Json,Jsp,我通过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', {

我通过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"));
        }
    });

您对使用console.logresponseData检查结果不感兴趣吗?您必须使用JSONArray并在whilers.next中向其添加一项{循环i.e.5次。另请参见以下问题:如何使用jQuery和jsp生成动态下拉列表?谢谢,但我将此标记为重复,因为您要做的是完全相同的,只是不同的触发器,加载时和更改时。@否,请不要将其标记为重复,我的问题很少与该问题匹配,我使用的是jsp not jstl tags非常感谢Nicola的回答,是的,我在第二个组合中只得到了5个,但是有3个错误出现了。1st:Map options=new Map;告诉你或者['expected.2nd:**options.addt1,t1;**告知找不到符号方法add.3rd:String json=new Gson.toJsonoptions;告知找不到符号类Gson。我认为您的代码需要稍加修改,您几乎不需要。请帮助我Nicola编辑代码,我正在尝试,但无法完成。@harry抱歉,我编辑了我的code我忘了..对于另一个错误,您必须导入Gson ithink@Nicola..please看一看,如果可能的话,请给出一个想法