Java 如何在选择选项的Map中显示第一个值,并在选择时获取第二个值?
我有MapJava 如何在选择选项的Map中显示第一个值,并在选择时获取第二个值?,java,jquery,jsp,servlets,Java,Jquery,Jsp,Servlets,我有Map,它们都是字符串,我用get方法将这个映射传递到jsp,并在选择选项列表中显示名称。但我想在选择名称并发送回Servlet时获取代码,因为我需要在sql中查询代码,名称可以在数据库中重复,但代码是唯一的。我该怎么做,有什么建议吗 我只使用jquery、servlet、jsp,请不要使用其他东西,比如Gson、Json、JSTL。此项目是有限的,不能使用任何其他LIB 这是我获取Map的示例代码,但在pro中,Map必须从数据库获取: JSP: Jquery: $(document).r
,它们都是字符串,我用get方法将这个映射传递到jsp,并在选择选项列表中显示名称。但我想在选择名称并发送回Servlet时获取代码,因为我需要在sql中查询代码,名称可以在数据库中重复,但代码是唯一的。我该怎么做,有什么建议吗
我只使用jquery、servlet、jsp,请不要使用其他东西,比如Gson、Json、JSTL。此项目是有限的,不能使用任何其他LIB
这是我获取Map的示例代码,但在pro中,Map必须从数据库获取:
JSP:
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);
}
谢谢你的帮助。也许这就是你要找的
将代码枚举的名称放在option元素的value属性中。通过给出您迄今为止所使用的一些代码,激发我们回答您的问题。我已经添加了一些代码。但是选择列表可以更改,因为我是从数据库获得的。无论如何,谢谢你的帮助。
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);
}
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>