回调后的jQuery.trigger()函数 [英] callback after jQuery.trigger() function

查看:387
本文介绍了回调后的jQuery.trigger()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题。我必须触发一个包含$ .post()的事件来加载表单并将其分配给DOM。完成后,我编辑窗体的字段。



我试过:

  
$('#type_rank_field')。trigger('change'); //调用$ .post()加载形式
})
.done(function children('option [value ='+ questions [i] .split('|')[0]); b $ b $('#quest _'+ questions [i] .split +']')。attr('selected',true);
});

不幸的是,这不工作,如果我离开它:

  $('#type_rank_field')。trigger('change'); 
$('#quest _'+ questions [i] .split('|')[1])。children('option [value ='+ questions [i] .split('|' ] +']')。attr('selected',true);

更改内容如下:

  $('#type_rank_field')。live('change',function(){
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);
});
});

然后,表单编辑在表单正确加载并附加到DOM之前执行。这可能是一个愚蠢的问题,JavaScript的家伙,但我主要是一个PHP的家伙,所以不要残忍: - )



感谢

更改处理程序代码?像这样:

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

然后代替触发更改只需调用 handleChange 传递一个回调即可在AJAX调用完成时执行:

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


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 form.

I tried:

$.when(function(){
    $('#type_rank_field').trigger('change'); //calls the $.post() to load the form
})
 .done(function(){
    $('#quest_'+questions[i].split('|')[1]).children('option[value="'+questions[i].split('|')[0]+'"]').attr('selected',true);
});

Unfortunately this doesnt work and if i leave it just like that:

$('#type_rank_field').trigger('change');
$('#quest_'+questions[i].split('|')[1]).children('option[value="'+questions[i].split('|')[0]+'"]').attr('selected',true);

The change even looks like this:

        $('#type_rank_field').live('change',function(){
        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);
        });
    });

Then the form editation is executed before the form is properly loaded and attached to DOM. This might be a stupid question for JavaScript guys, but i am mainly a PHP guy so dont be cruel :-)

Thanks

解决方案

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

这篇关于回调后的jQuery.trigger()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆