2011-08-03 82 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