间歇性地选择Dropdown并不设置值

时间:2018-01-24 09:19:08

标签: jquery ajax asp.net-mvc-4

我在页面上有几个下拉选项,这些选择都是从AJAX调用中填充的。其中每一个都有一个默认选项集(这是最终用户的“编辑”页面),但是我注意到随机下拉将间歇性地没有设置它们的值。 它发生在不同的下拉列表和不同的记录集中,我不知道为什么或如何编写页面以确保在呈现视图之前已经正确设置了所有页面? 下面的代码示例......

下拉菜单:

    <div class="form-group row" id="d2">
        <label for="" class="col-sm-2 col-form-label required">Business Owner</label>
        <div class="col-lg-6">
            <select class="form-control" id="cmb1">
                <option value="" selected>-Select-</option>
            </select>
        </div>
    </div>

AJAX调用填充:

function FillDD1() {
            $.ajax({
                url: '@Url.Action("/NewMeasure1")/',
                type: "GET",
                success: function (qx) {
                    var trHTML = '';
                    $.each(qx, function (i, item) {
                        trHTML += '<option value="' + item.people_PK + '">' + item.name + '</option>';
                    });
                    $('#cmb1').append(trHTML);
                },
                error: function () {
                    var trHTML = '';
                    trHTML = 'Error fetching data';
                    $('#cmb1').append(trHTML);
                }
            })
        };

设置值的代码:

function FetchMeasureInfo() {
            $.ajax({
                url: '@Url.Action("/FetchMeasureInfo")/',
                type: "GET",
                data: {
                    measurekey: getSessionInfo
                },
                success: function (data) {
                    var count = Object.keys(data).length;
                    console.log(count);
                    if (count == 0) {
                        msg = "AJAX Error. Server responded but with an incomplete recordset for Measure PK " + getSessionInfo
                        AddErrorLog();
                        $('#nodata').fadeIn('slow');
                        $('#form').hide();
                    }

                    $("#cmb1").val(data[0].BusinessOwnerKey).change();
                    $("#cmb1a").val(data[0].GatekeeperKey).change();
                    $("#cmb2").val(data[0].availabilityKey).change();

                    var troy = $('#cmb4 :selected').val()

                    if (troy == '') {
                        $('#missinginfo').slideDown('slow');
                        msg = "Alert for missing info shown. Saving edits may not work correctly."
                        AddErrorLog();
                    }
                },
                error: function () {
                    msg = "The database could not be found. Possible network issue."
                    AddErrorLog();
                }
            })
        };

它被称为:

$(window).on("load", function() {
            if (sessionStorage.length == 0) {
                $('#form').hide();
                $('#noway').slideDown('slow');
                msg = "Bad Request. User likely loaded page without referrer"
                AddErrorLog();
            }
            else {
                getSessionInfo = sessionStorage.getItem('measure');
                FillDD1();
                FillDD2();
//FillDD3-9 go here
                FetchMeasureInfo();
                $('#form').fadeIn('slow');
                sessionStorage.removeItem('measure');
            }
        });

为了简洁起见,我删除了所有下拉列表代码(它们的ID完全不同)

0 个答案:

没有答案