【问题标题】:How to append data on a select tag through ajax in Spring mvc如何在 Spring mvc 中通过 ajax 在选择标签上附加数据
【发布时间】:2018-11-12 08:04:35
【问题描述】:

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

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

我的代码如下所示:

UserController.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";
}

并且 更新用户.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))
                });
            }
        });
    }

【问题讨论】:

    标签: javascript jquery html ajax spring-mvc


    【解决方案1】:

    我自己解决了这个问题。我们应该在 javascript 中使用 IIFE(Immediately Invoked Function Expressions) 函数。

    【讨论】:

      猜你喜欢
      • 2018-04-11
      • 2021-10-29
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 2016-06-27
      • 1970-01-01
      相关资源
      最近更新 更多