Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在SpringMVC中通过ajax将数据附加到select标记上_Javascript_Jquery_Html_Ajax_Spring Mvc - Fatal编程技术网

Javascript 如何在SpringMVC中通过ajax将数据附加到select标记上

Javascript 如何在SpringMVC中通过ajax将数据附加到select标记上,javascript,jquery,html,ajax,spring-mvc,Javascript,Jquery,Html,Ajax,Spring Mvc,我正在做一个小的SpringMVC项目,我保存了用户数据。在页面中,当我单击edit the Country data时,从后端填充并应用Ajax Onchange事件 当管理员选择国家时,状态列表追加,当管理员选择状态时,区域列表追加,等等。但是,当我点击编辑国家数据和没有国家,地区数据附加。我还尝试在加载时运行Ajax。有人能帮我吗 我的代码如下所示: UserController.java: 及 updateuser.jsp <label>Country*<

我正在做一个小的SpringMVC项目,我保存了用户数据。在页面中,当我单击edit the Country data时,从后端填充并应用Ajax Onchange事件

当管理员选择国家时,状态列表追加,当管理员选择状态时,区域列表追加,等等。但是,当我点击编辑国家数据和没有国家,地区数据附加。我还尝试在加载时运行Ajax。有人能帮我吗

我的代码如下所示:

UserController.java:

及 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中使用即时调用的函数表达式

<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))
                });
            }
        });
    }