简体   繁体   中英

How can I pass parameter when trigger?

When my JavaScript is run it will call the getDoctorComplete function. When the AJAX request is completed, it will set a = chelsea . Otherwise, there will be a trigger change and will run $('#dropdown_hosp').change(... . In the $('#dropdown_hosp').change(... , it will call the getSpecialty function. When the AJAX request is completed with the getSpecialty function, I want to get the value of a . I console.log it, but it contains an empty string. How can I solve this problem?

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

Your problem ist, that your ajax call is async and therefore a is not reassigned when being logged. Here would be a solution to your problem using Promise , to create an async function, resolve , to mark the return of that async function and .then() to do something after that async function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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