0

我正在制作一个小型 Spring MVC 项目并保存用户数据。在页面中,当我单击从后端编辑 Country 数据填充并应用 Ajax Onchange 事件时。

当管理员选择国家时,追加州列表,当管理员选择州时,追加地区列表等等。但是当我点击编辑国家数据和无州时,附加地区数据。我还尝试在负载时运行 Ajax。谁能帮我?

我的代码如下所示:

用户控制器.java

@RequestMapping(value = "/editApplicant/{id}", method = RequestMethod.GET)
public String editUser(@PathVariable("id") int id, ModelMap modelMap) {
    UserDataPojo userDataPojo = userDataService.getDetailsById(id);
    List<DestinationPojo> destinationPojos = destinationService.getAllCountries();

    modelMap.addAttribute("countries", destinationPojos);
    modelMap.addAttribute("applicantData", userDataPojo );
    if (applicantDataPojo == null) {
        modelMap.addAttribute("Msg", "User is Not Found");
    }
    return "updateuser";
}

并更新user.jsp

        <label>Country*</label>
        <form:select class="form-control"
         onChange="AjaxCallStateperma()" id="parmanentCountry" 
         path="parmanentCountry">
         <form:option value="${null}">Select</form:option>
         <form:options items="${countries}" itemLabel="countryName" 
         itemValue="countryId" />
         </form:select>


        <label>State*</label>
        <form:select class="form-control"
         onChange="ajaxcalldistrictperma()" id="parmanentState" 
         path="parmanentState">
        <form:option value="${null}">Select</form:option>
        </form:select>

我的ajax是

<script type="text/javascript">
    // business Address to list start
    function AjaxCallState() {
        var country = $('#businessCountry').val();
        console.log(country);
        $.ajax({
            url : "${pageContext.request.contextPath}/loadState",
            async : false,
            type : "GET",
            data : "state=" + country,

            contentType : "application/json;charset=UTF-8",
            dataType : "json",
            success : function(data) {
                $('#businessState').empty();
                $('#businessState').append(
                        '<option value="null">Select State</option>');
                $.each(data, function(i, value) {
                    $('#businessState').append(
                            $('<option>').text(value.stateName).attr(
                                    'value', value.stateId))
                });
            }
        });
    }
4

1 回答 1

0

我自己解决了这个问题。我们应该在javascript中使用IIFE(立即调用函数表达式)函数。

于 2018-06-09T11:24:40.693 回答