简体   繁体   English

jQuery-下一行代码后Ajax调用完成

[英]JQuery - Ajax call completes after next line of code

I have the following ajax function which dynamically adds options to a dropdown list. 我有以下ajax函数,可将选项动态添加到下拉列表中。

var getData = function() {

    $.getJSON('api/someurl')
            .done(function (data) {

                console.log(data);


                var results = $("#Mydropdown")
                              .empty()
                              .append("<option value=''>Please select</option>");

               // loop data and build list
            })
            .fail(function (jqXHR, textStatus, err) {

            });
};

If I already have a value selected which I hold in some input field, then on page load I want to recreate the list and set the selected option to what was previously selected. 如果我已经选择了一个保留在某个输入字段中的值,那么在页面加载时,我想重新创建列表并将所选选项设置为先前选择的值。

The following doesn't work. 以下无效。 In the console I can see the ajax data is being returned after this line executes $('#Mydropdown').val($("#SelectedVal").val()); 在控制台中,我可以看到在此行执行$('#Mydropdown').val($("#SelectedVal").val());之后返回了ajax数据$('#Mydropdown').val($("#SelectedVal").val()); so the value isn't getting selected? 所以值没有被选中?

if ($("#SelectedVal").val()) {
    getData(); // this function logs to console too
    $('#Mydropdown').val($("#SelectedVal").val());
    console.log('done');
}

How do I fix? 我该如何解决?

* Note * I call the getData function in other functions as well which is why I don't pass the selected value in the above scenario in the function. *注意*我在其他函数中也调用了getData函数,这就是为什么在上述情况下我不传递所选值的原因。

You need to set the selected value after updating the dropdown inside the success callback: 更新成功回调中的下拉列表后,需要设置所选值:

var selectedValue = $("#SelectedVal").val();
if (selectedValue) {
    getData(selectedValue);
}

and then: 接着:

var getData = function(selectedValue) {
    $.getJSON('api/someurl').done(function (data) {
        var results = $("#Mydropdown")
            .empty()
            .append("<option value=''>Please select</option>");

        // loop data and build list

        // after you have built the new list set the selected value
        // to what it previously was
        $('#Mydropdown').val(selectedValue);
    })
    .fail(function (jqXHR, textStatus, err) {
    });
};

Alternatively if you don't want to set the selected value inside the getData function, you could use a callback: 另外,如果您不想在getData函数中设置选定的值,则可以使用回调:

var selectedValue = $("#SelectedVal").val();
if (selectedValue) {
    getData(function() {
        $("#SelectedVal").val(selectedValue);
    });
}

and your getData function might look like this: 并且您的getData函数可能如下所示:

var getData = function(callback) {
    $.getJSON('api/someurl').done(function (data) {
        var results = $("#Mydropdown")
            .empty()
            .append("<option value=''>Please select</option>");

        // loop data and build list

        // after you have built the new list invoke the callback
        callback();
    })
    .fail(function (jqXHR, textStatus, err) {
    });
};

you should select value in the callback of getdata success here: see attached code here: 您应该在此处获取getdata成功的回调中选择值 :请参见此处的附加代码:

       $.getJSON('api/someurl')
        .done(function (data) {


            var results = $("#Mydropdown")
                          .empty()
                          .append("<option value=''>Please select</option>");

           // loop data and build list
           //After List Build is Completed Select Drop Down Here

            $('#Mydropdown').val($("#SelectedVal").val());

            console.log('done');

        })
        .fail(function (jqXHR, textStatus, err) {

        });

Maybe use doneCallback function for example; 也许使用doneCallback函数为例;

Use callback 使用回调

var getData = function(doneCallback) {

    $.getJSON('api/someurl')
            .done(function (data) {

                console.log(data);


                var results = $("#Mydropdown")
                              .empty()
                              .append("<option value=''>Please select</option>");

               // loop data and build list
               if(doneCallback)
                doneCallback();
            })
            .fail(function (jqXHR, textStatus, err) {

            });
};

Define callback 定义回调

if ($("#SelectedVal").val()) {
    getData(function(){
        $('#Mydropdown').val($("#SelectedVal").val());
    });
    console.log('done');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM