我有地圖<代碼,名稱>,他們都是字符串,我把這個地圖通過get方法傳遞給jsp,並在選擇選項列表中顯示名稱。但我想要的代碼,當我選擇名稱和發回到Servlet,因爲我將需要代碼來查詢在SQL中,名稱可以在數據庫中重複,但代碼是獨特的。我可以做什麼,對我的任何建議?如何在選擇選項中顯示Map <String,String>中的第一個值並在選擇時獲取第二個值?
我只是使用jquery,servlet,jsp,請不要使用其他東西,比如Gson,Json,JSTL。該項目是有限的,不能使用任何其他庫。
這是我的示例代碼來獲得地圖<代碼,名稱>但在親地圖必須從數據庫中獲取: JSP:
<select id="selectgroup" name="selectgroup">
<option>Group1</option>
<option>Group2</option>
</select>
<select id="selectcity" name="selectcity">
</select>
的Jquery:
$(document).ready(function(){
$("#selectgroup").change(function(event){
var text='';
var name = $("#selectgroup").val();
$.get('Select', {
comName : name
}, function(responseText) {
$("#selectcity").empty();
var listEmp = responseText.replace("{", "").replace("}", "");
var arrayEmp = listEmp.split(", ");
arrayEmp.forEach(function(emp){
var city = emp.split("=");
if(city[1] != undefined){
$("#selectcity").append(
'<option value="' + city[1] + '">' + city[1] + '</option>'
);
}
});
});
});
});
和servlet:
String comNameChanged = request.getParameter("comName")+"";
if(comNameChanged.equals("Group1")){
Map<String, String> ind = new HashMap<String, String>();
//List<String> ind = new ArrayList<String>();
ind.put("001","New delhi");
ind.put("002","Tamil Nadu");
ind.put("004","Kerala");
ind.put("005","Andhra Pradesh");
response.setContentType("text/plain");
response.getWriter().print(ind);
}
if(comNameChanged.equals("Group2")) {
Map<String, String> us = new HashMap<String, String>();
//List<String> ind = new ArrayList<String>();
us.put("001","NewYork");
us.put("002","Hawai");
us.put("004","Test");
us.put("005","Cali");
response.setContentType("text/plain");
response.getWriter().print(us);
}
感謝您的幫助。
鼓舞我們回答你的問題,給出你迄今爲止工作過的一些代碼。 –
我添加了一些代碼。 – thefriend