2012-02-12 79 views
0

我通過JSON數組獲取一個值,但是如何將多個值存儲在JSON數組中,以及如何通過JavaScript檢索它?通過JSON數組對象檢索多個值

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()); 
     } 
%> 
+0

你沒有興趣用console.log(responseData)檢查結果嗎? – bondythegreat 2012-02-12 11:34:07

+0

您必須使用'JSONArray'並在'while(rs.next()){'循環內添加一個項目(即5次)。 – 2012-02-12 11:34:14

+0

另請參閱此問題:*如何使用jQuery和jsp生成動態下拉列表?http://stackoverflow.com/questions/2896730/how-to-generate-dynamic-drop-down-lists-using-jquery-和-jsp – 2012-02-12 11:37:58

回答

1

我想是因爲你在你的循環犯錯誤你只得到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")); 
     } 
    }); 
+0

非常感謝尼古拉的回答,雅我只得到5秒的組合,但3錯誤即將到來。第一種:** Map options = new Map ; **告訴'('或'['expected。第二種:** options.add(t1,t1); **告訴無法找到符號方法添加第3:**字符串json =新的Gson()。toJson(選項); **告訴我找不到符號類Gson。我認爲你的代碼需要稍微修改,你幾乎完成 – harry 2012-02-13 09:57:04

+0

請幫助我Nicola通過編輯你的代碼,我試圖但不能這樣做 – harry 2012-02-13 10:05:54

+0

@harry對不起我編輯我的代碼我忘了()..對於其他錯誤,你必須導入Gson我認爲 – 2012-02-13 11:31:36