回调后的jQuery.trigger()函数 [英] callback after jQuery.trigger() function
问题描述
我有一个小问题。我必须触发一个包含$ .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屋!