callback after jQuery.trigger() function

后端 未结 4 732
遥遥无期
遥遥无期 2021-01-12 08:00

i have got a little problem here. I have to trigger an event which contains $.post() to load a form and assign it to a DOM. After this is done, i have edit the fields of the

4条回答
  •  忘掉有多难
    2021-01-12 08:35

    Can separate out your change handler code? Something like this:

    $('#type_rank_field').on('change',function(){
        handleChange($(this));
    });
    
    function handleChange(elem, callback) {
        var id = elem.children('option:selected').attr('id');
        var id_edited = get_id_from_id(id);
        $.post('ajax/load_questions_of_rank.ajax.php',{id: id_edited},function(data){
            //alert(data);
            $('#rank_fields').html(data);
            if (typeof callback === "function") {
                callback(data);
            }
        });
    };
    

    Then instead of triggering the change you can just call handleChange passing a callback to execute when the AJAX call is complete:

    handleChange($("#type_rank_field"), function(data) {
        $('#quest_'+questions[i].split('|')[1])
            .children('option[value="'+questions[i].split('|')[0]+'"]')
            .attr('selected',true);
    });
    

提交回复
热议问题