【问题标题】:How can I pass parameter when trigger?触发时如何传递参数?
【发布时间】:2020-01-26 11:08:33
【问题描述】:

当我的 JavaScript 运行时,它会调用 getDoctorComplete 函数。当 AJAX 请求完成时,它会设置a = chelsea。否则,将发生触发器更改并运行$('#dropdown_hosp').change(...。在$('#dropdown_hosp').change(... 中,它会调用getSpecialty 函数。当用getSpecialty函数完成AJAX请求后,我想得到a的值。我console.log 它,但它包含一个空字符串。我该如何解决这个问题?

var a = '';
$(document).ready(function () {
    app.getHospital({
        areaId: ''
    });

    app.getDoctorComplete({
        doctorId: doctorId,
        doctorel: $('#dropdown_doct')
    });

    $('#dropdown_hosp').change(function () {
        var dropdownspecialty = $('#dropdown_spec');
        app.getSpecialty({
            hospitalId: $('#dropdown_hosp').val(),
            apiUrl: 'api',
            special: dropdownSpec
        });
    });
});

app = function () {
    function getHospital({
        areaId
    }) {
        // ...
        $.ajax({
            // ...
            success: function (result) {
                // ...
            },
            // ...
        }).done(function () {
            $('#dropdown_hosp').select2();
        });
    };

    function getSpecialty({
        hospitalId,
        apiUrl,
        special
    }) {
        // ...
        $.ajax({
            // ...
        }).done(function () {
            // test here
            console.log(a);
        });
    };

    function getDoctorComplete({
        schdoctor_id,
        doctorel
    }) {
        // ...
        $.ajax({
            // ...
            success: function (result) {
                // ...
            },
            // ...
        }).done(function () {
            // ...
            a = 'chelsea';
            b = '..';
            $('#dropdown_hosp').val(b).trigger('change');
        });
    };
    return {
        getSpecialty: getSpecialty,
        getDoctorComplete: getDoctorComplete
    }
}();

【问题讨论】:

  • 您需要将 ajax 函数包装在 Promise 中,在 done 方法中解析该 Promise 并在调用后调用 .then(),如 app.getDoctorComplete( ... ).then(() => { Do something after the call here })
  • 尝试添加回调,也许会有帮助

标签: javascript jquery ajax triggers parameter-passing


【解决方案1】:

您的问题在于,您的 ajax 调用是异步的,因此在记录时不会重新分配 a。这是使用Promise 来解决您的问题的解决方案,创建一个异步函数resolve,以标记该异步函数的return,并使用.then() 在该异步函数之后执行某些操作:

var a = '';
$(document).ready(function () {
    app.getHospital({
        areaId: ''
    });

    app.getDoctorComplete({
        doctorId: doctorId,
        doctorel: $('#dropdown_doct')
    })
        .then(() => { // Add the .then() callback to do something after getDoctorComplete finished
            $('#dropdown_hosp').change(function () {
                var dropdownspecialty = $('#dropdown_spec');
                app.getSpecialty({
                    hospitalId: $('#dropdown_hosp').val(),
                    apiUrl: 'api',
                    special: dropdownSpec
                });
            });
        });
});

app = function () {
    function getHospital({
        areaId
    }) {
        return new Promise((resolve, reject) => {
            // ...
            $.ajax({
                // ...
                success: function (result) {
                    // ...
                },
                // ...
            }).done(function () {
                $('#dropdown_hosp').select2();

                resolve(); // Add resolve
            });
        });
    };

    function getSpecialty({
        hospitalId,
        apiUrl,
        special
    }) {
        return new Promise((resolve, reject) => {
            // ...
            $.ajax({
                // ...
            }).done(function () {
                // test here
                console.log(a);

                resolve(); // Add resolve
            });
        });
    };

    function getDoctorComplete({
        schdoctor_id,
        doctorel
    }) {
        return new Promise((resolve, reject) => {
            // ...
            $.ajax({
                // ...
                success: function (result) {
                    // ...
                },
                // ...
            }).done(function () {
                // ...
                a = 'chelsea';
                b = '..';
                $('#dropdown_hosp').val(b).trigger('change');

                resolve(); // Add resolve
            });
        });
    };
    return {
        getSpecialty: getSpecialty,
        getDoctorComplete: getDoctorComplete
    }
}();

【讨论】:

  • 有更简单的方法吗?因为我的情况很复杂。怕影响别人
  • @SuccessMan -- 理论上,您可以将(回调)函数作为参数传递给每个函数,并在 done 块内调用它。除此之外,我还没有想到安全的解决方案。这就是异步代码的工作原理,你应该习惯它,因为在处理 Ajax 请求时无法绕过它(Ajax 这个词的字面意思是“异步 JavaScript 和 XML”
  • @SuccessMan 如果这解决了您的问题,请接受答案并随时给予支持。如果没有,请提供更多信息。
猜你喜欢
  • 2020-07-18
  • 1970-01-01
  • 2020-04-23
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 2017-02-03
  • 2012-06-01
  • 2021-05-08
相关资源
最近更新 更多