callback after jQuery.trigger() function

后端 未结 4 687
遥遥无期
遥遥无期 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:34

    .live has been deprecated in jQuery since v1.7, and has been removed in v1.9.

    You should replace it with .on().

    .on has 2 signatures for binding elements, whereas .live only had 1.

    If the element exists at the time you are binding, you do it like this:

    $('.element').on('click', function(){
      .......
    });
    

    You can even use the shorthand:

    $('.element').click(function(){
      .........
    });
    

    If the element does not exist at the time, or new ones will be added (which is what .live was normally used for), you need to use "event delegation":

    $(document).on('click', '.element', function(){
      ........
    });
    

    NOTE: You want to bind to the closest static element, not always document.

    In the meantime, the jQuery Migrate plugin can be used to restore the .live() functionality if you upgrade your jQuery to the newest version.

    0 讨论(0)
  • 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);
    });
    
    0 讨论(0)
  • 2021-01-12 08:35

    Return the promise object from your event handler:

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

    and then use triggerHandler() instead.

    var promise = $('#type_rank_field').triggerHandler('change');
    promise && promise.done(function(){
        // do stuff
    });
    

    Here's a simple example showing the functionality being used: http://jsfiddle.net/WQPXt/

    0 讨论(0)
  • 2021-01-12 08:39

    I think we have to add callback after posted

    $('#type_rank_field').on('change', function(ev, cb){
        var id = $(this).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);
            // add after callback to make sure that html is inserted
            if(typeof cb == "function"){
               cb.apply($(this)) // this apply with the jq object context or another context u want
            }
     });
    

    the trigger change will look like this

    $('#type_rank_field').trigger('change', [function(){
      $('#quest_'+questions[i].split('|')[1]).children('option[value="'+questions[i].split('|')[0]+'"]').attr('selected',true);
    }]);
    
    0 讨论(0)
提交回复
热议问题