如何在选择选项中的Map <string,string =“”>中显示第一个值,并在选择时获得第二个值?

时间:2015-11-04 16:25:06

标签: java jquery jsp servlets

我有Map&lt;代码,名称&gt;,它们都是String,我使用get方法将此Map传递给jsp,并在select选项列表中显示Name。但我想在我选择名称并发送回Servlet时获取代码,因为我需要在sql中查询代码,名称可以在数据库中重复但代码是唯一的。我怎么能这样做,对我有什么建议?

我只是使用jquery,servlet,jsp,请不要使用其他东西,如Gson,Json,JSTL。该项目有限,不能使用任何其他库。

这是我获取Map&lt;的示例代码代码,名称&gt;但在Pro Map中必须从数据库中获取: 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);         
            }

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

也许这就是你要找的东西?

http://www.w3schools.com/tags/tag_select.asp

 <select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select> 

将Code enum的名称放入option元素的value属性中。