是否有可能动态地重新加载表单:选择值取决于另一个表单的值:select [英] Is it possible to dynamically reload a form:select with values depending of the value of another form:select

查看:107
本文介绍了是否有可能动态地重新加载表单:选择值取决于另一个表单的值:select的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对州/市下拉选择的形式。城市下拉应根据用户选择的状态动态更改。我在Spring MVC中使用了jQuery。

我的状态/城市对象是一个HashMap的HashMap,所以对于状态'01'(第一个关键字)已获得城市001(第二键) - city1(value)和002(second key) - city2(value):

  LinkedHashMap< ; String,LinkedHashMap< String,String>> enumsCountyByDistrict = new LinkedHashMap< String,LinkedHashMap< String,String>>(); 
LinkedHashMap< String,String> districtCounties = new LinkedHashMap< String,String>(); (城市en:城市)

districtCounties.put(en.getCode(),en.getDescription());

enumsCountyByDistrict.put(district,districtCounties);

其中cities是我从数据库中检索的列表。



我将这个对象传递给我的视图:

$ p $ model $ and $ view $ ;
modelAndView.addObject(区,区);

其中区域是不同州的列表。



现在,我的JSP以表单显示值:选择:

 < div class =span3> 
< label> < fmt:message key =create.district/>< / label>
< form:select id =addressdistrictpath =person.addressdistrict>
< c:forEach items =$ {districts}var =item>
< form:option value =$ {item.code}label =$ {item.description}/>
< / c:forEach>
< / form:select>
< / div>
< div class =span3>
< label> < fmt:message key =create.county/> < /标签>
< form:select path =person.addresscountyid =addresscounty>
< form:options items =$ {countiesByDistrict ['13']}/>
< / form:select>
< / div>

我将hardcodding县的分区[ '13']显示为城市13区,它确实,但现在,显然,我希望它改变取决于在addressdistrict形式选择的代码:select。

任何人都可以提供帮助吗?

解决方案

城市列表并更新您的其他选择框。如果您不想进行ajax调用或者城市列表很小,请在初始页面请求中获取这两个细节,并使用javascript / jquery进行更新。
检查这个问题 jquery-json-populate-dropdown-list


I've a pair of state/city drop down selects in a form. The city drop down should be dynamically changed depending of the state selected by the user. I'm using jQuery with Spring MVC.

My object of states/cities is a HashMap of HashMaps, so, for state '01' (first key), I've got cities 001 (second key) - city1 (value) and 002 (second key) - city2 (value):

LinkedHashMap<String,LinkedHashMap<String, String>> enumsCountyByDistrict = new LinkedHashMap<String,LinkedHashMap<String, String>>();
LinkedHashMap<String, String> districtCounties = new LinkedHashMap<String, String>();
for (City en : cities)
    districtCounties.put(en.getCode(), en.getDescription());

enumsCountyByDistrict.put(district, districtCounties);

where cities is a list which I retrieve from database.

I'm passing this object to my view with:

modelAndView.addObject("countiesByDistrict", enumsCountyByDistrict);
modelAndView.addObject("districts", districts);

Where districts is the lists of the different states.

Now, my JSP shows the values with form:selects :

        <div class="span3">
            <label> <fmt:message key="create.district" /></label>
            <form:select id="addressdistrict"  path="person.addressdistrict">
                <c:forEach items="${districts}" var="item">
                    <form:option value="${item.code}" label="${item.description}" />
                </c:forEach>
            </form:select>
        </div>
        <div class="span3">
            <label> <fmt:message key="create.county" /> </label>
            <form:select path="person.addresscounty" id="addresscounty">
                <form:options items="${countiesByDistrict['13']}" />
            </form:select>
        </div>

I'm hardcodding countiesByDistrict['13'] to show the cities of district 13, and it does it ok, but now, obviously, I want it to change depending of the code selected at addressdistrict form:select.

Anyone can help?

解决方案

Make an ajax call to get the list of cities and update your other select box. If you don't want to do ajax call or the list of cities is small fetch both details in initial page request and use javascript/jquery to update. Check this question jquery-json-to-populate-dropdown-list,

这篇关于是否有可能动态地重新加载表单:选择值取决于另一个表单的值:select的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆