jQuery ajax()内for循环

时间:2019-04-06 05:48:28

标签: javascript jquery arrays ajax for-loop

我正在剃刀视图上实现默认的编辑模式。除了下拉列表的填充功能之外,所有其他功能均有效。关于我的工作,我必须在加载事件时根据当前所选国家/地区的值来填写我的州下拉列表。为了实现,我已经在for循环中实现了ajax。

剃刀

@foreach (CustomerModel customer in Model)
    {

        <tr>
            <td class="CustomerId">
                <span id="spn_@customer.CustomerId">@customer.CustomerId</span>
            </td>
            <td class="Name">
                @Html.TextBoxFor(model => customer.Name,new { id =  "txtName_" + @customer.CustomerId , onkeyup = "getID(this.id)" })
                @*<input id="txtName_@customer.CustomerId" type="text" onkeyup="getID(this.id)" value="@customer.Name"/>*@
            </td>
            <td class="State">
                <select id="ddlState_@customer.CustomerId" style="width:200px;">
                    @*<option>@customer.StateName</option>*@
                </select>
            </td>

            <td class="Country">
               <select id="ddlCountry_@customer.CustomerId" onfocus="fillCountry(this.id)" onchange="fillState(this.id,this.value)" style="width:200px;">
                   <option>@customer.CountryName</option>
               </select>
            </td>
           <td>
               <button id="@customer.CustomerId" onclick="update(this.id)">Save</button>
               </td>
        </tr>
          <script>
              arrayID.push('@customer.CustomerId');
            </script>
    }

脚本

    var ids;
    var txtName;
    var txtCountry;
    var onchangeFlag = 0;
    s = 0;
    var idOf;
    var idArray = [];
    var length = 2;
    var flag = 1;
    $(document).ready(function () {
       // alert(arrayID);
        var ido;
        var ddlCountryID;
        var index = 0;
        var countryValue;
        var ddlStateidElement;
        var s = 0;
        var ddlStateID;
        var lenthof = arrayID.length;
        for (var i = 0; i < lenthof; i++) {
           // alert(lenthof);
            ido = arrayID[i];
            //index = ido;
            ddlCountryID = "ddlCountry_" + ido;
            ddlStateID = "ddlState_" + ido;

            countryValue = $('#' + ddlCountryID).val();
           // alert(countryValue);
            $.ajax({
                type: "POST",
                url: "/New/GetOnState",
                contentType: "application/json; charset=utf-8",
                data: '{"country":"' + countryValue + '"}',
                dataType: "html",
                success: function (result, status, xhr) {
                    //  alert("success");

                   // alert(arrayID[index]);

                    ddlStateidElement = "ddlState_" + ido;
                    $('#' + ddlStateidElement).html(result);
                    //index = index + 1;

                },
                error: function (xhr, status, error) {
                    alert("Enter");

                }
            });
           //loopend
        }

c#代码和ajax始终都成功,但是循环索引始终指向数组的最后一个索引,我不知道为什么循环索引失败。

输出

enter image description here

1 个答案:

答案 0 :(得分:1)

 $.ajax({
                type: "POST",
                url: "/New/GetOnState",
                contentType: "application/json; charset=utf-8",
                data: '{"country":"' + countryValue + '"}',
                dataType: "html",
                async:false,
                success: function (result, status, xhr) {
                    //  alert("success");

                   // alert(arrayID[index]);

                    ddlStateidElement = "ddlState_" + ido;
                    $('#' + ddlStateidElement).html(result);
                    //index = index + 1;

                },
                error: function (xhr, status, error) {
                    alert("Enter");

                }
            });