【问题标题】:Populate State dropdown based on the selection of country dropdown in java spring基于java spring中国家下拉列表的选择填充状态下拉列表
【发布时间】:2017-10-16 07:19:55
【问题描述】:

在我的控制器中,我写了如下内容来填充国家列表和州列表:

@ModelAttribute("countries")
public List<Country> initializeCountries() {

    List <Country> countries = countryService.findAllCountrys();

    return countries;
}   

@ModelAttribute("状态") 公共列表 initializeStates() {

List <State> states = stateService.findAllStates();

   return states;

}

在我的 jsp 中,我成功地填充了国家和州:

<form:select path="country" id="country">
  <form:option value="">Select Country</form:option>
  <form:options items="${countries}" itemValue="countryCode" itemLabel 
     = "countryName" />
</form:select>
<form:errors path="country" cssClass="error"/>

<form:select path="state" id="state">
    <form:option value="">Select Country</form:option>
    <form:options items="${states}" itemValue="stateCode" 
  itemLabel="stateName" />
</form:select>
<form:errors path="state" cssClass="error"/>

但我需要根据国家/地区的选择来填充状态。

【问题讨论】:

  • 你必须使用javascript来实现这个。

标签: java spring jsp drop-down-menu combobox


【解决方案1】:

您可以使用 Ajax 和 JavaScript/JQuery。这里举个例子,可能对你有帮助

$("#country").change(function() {
    var countryId = $(this).val();
    $.ajax({
        async: true,
        type: 'GET',
        url: '/fetchStates',
        data: {
            countryId : countryId,
        },
        error: function() { 
            alert("Error");
        },
        success: function(stateDetails) {
            // call a function to populate state details
        }
    });
});

【讨论】:

  • 请检查下面的代码,让我知道你的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 2015-05-20
  • 1970-01-01
相关资源
最近更新 更多