2011-08-03 80 views
0

这里有一个奇怪的局面.....AJAX功能和异步:假

我得到不能编辑功能(出于某种原因,我需要使用它,而不是创建一个新的1,或覆盖。脚本是不允许的......)

function update_tpl2(form, divtoupdate, exeAlert) { 
if(!$('#'+form).length) 
    form = 'myform'; 
if(!$('#'+divtoupdate).length) 
    divtoupdate = 'ajax_update'; 

$.ajax({ 
    type: "POST", 
    url: url, 
    data: $('#' + form).serialize(), 
    dataType: "html", 
    beforeSend: ShowLoading, 
    success: function(resp){ 
     $('#theLoading').dialog('close'); 
     $('#loading').html('');   
     $('#' + divtoupdate).html(resp); 
    } 
}); 

}

,我需要添加异步:假到该功能时运行。 有没有什么办法将ajax设置为async:false,通过不改变功能并使用它.....

回答

0

也许这样的事情?

只需要调用与性能=下面的函数{异步:假}

function update_tpl2(form, divtoupdate, exeAlert, properties) { 
if(!$('#'+form).length) 
    form = 'myform'; 
if(!$('#'+divtoupdate).length) 
    divtoupdate = 'ajax_update'; 

var defaultProps = { 
    type: "POST", 
    url: url, 
    data: $('#' + form).serialize(), 
    dataType: "html", 
    beforeSend: ShowLoading, 
    success: function(resp){ 
     $('#theLoading').dialog('close'); 
     $('#loading').html('');   
     $('#' + divtoupdate).html(resp); 
    } 
} 

jQuery.extend(true, defaultProps, properties) 

$.ajax(defaultProps); 
+0

但你改变update_tpl2功能DY。这不应该是^^而是尝试 – Leon

+0

aye,你改变它的内容,但逻辑应该保持不变。这样你可以使用属性输入参数来改变行为。如果这确实是不可能的,那么可能有另一种方式。但它是一个可怕的解决方案!你可以做一些设置'jQuery.ajaxSetup({async:false}); update_tpl2(.....); jQuery.ajaxSetup({异步:真})'。它会首先将async:false设置为默认值,然后调用您的函数,然后在完成时重置为async:true。更多在这里:http://api.jquery.com/jQuery.ajaxSetup – netbrain