2012-12-23 83 views
0

我試圖開發一個互動網站後不會觸發和我被困在其中一個文件應該被添加和executed.So我在扉頁以下代碼點:Javascript代碼加載

$(document).ready(function(){ 

$('.info').live("click",function() 
     { 
      if ($('#country span.selected').text().length == 0) 
       alert("A company should be selected before continue!"); 
      else { 
       $('.centered').hide(); 
       $('.waitimg').show(); 
       $.post('get_data.php', 
       { 
        type :$(this).attr("id"), 
        company : $('#country span.selected').text(), 
        company_id : $('#country').attr("value"), 
        country : $('#scountry span.selected').text(), 
        industry: $('#industry span.selected').text() 
       }, function(response) 
       { 
        //$('#resultarea').fadeOut(); 
        setTimeout("finishAjax('resultarea','"+escape(response)+"')", 400); 
       } 
       ); 
      } 
      return false;      
     }); 

    }); 

和回調:

function finishAjax(id, response) { 
     $('#waitimg').hide(); 
     $.getScript("js/script.js"); 
     $('#'+id).html(unescape(response)); 
     $('#'+id).fadeIn(); 
    } 

它應該執行從JS /的script.js腳本,但事實並非如此。使用Firebug我可以看到文件被加載,但是在script.js中沒有被觸發,只是處於簡單警報狀態(「hello world」)。爲什麼?

回答

1

setTimeout預計參考函數,而不是作爲第一個參數的字符串。替換:

setTimeout("finishAjax('resultarea','"+escape(response)+"')", 400); 

有:

setTimeout(function() 
{ 
    return finishAjax.apply(this, ['resultarea', escape(response)]);//call as though finishAjax was callback --> preserve context 
    //or, if you don't care about the context: 
    return finishAjax('resultarea', escape(response)); 
}, 400); 

大功告成

+0

嘗試,這是行不通的。 –

+0

@StackOverfolow:我不是精神病患者,所以我不知道你的頁面是什麼樣的,或者其他代碼可能導致問題。 [嘗試設置一個小提琴](http://jsfiddle.net),所以我們可以看到你已經嘗試 –

+0

響應是好的,我的問題是,script.js中的代碼根本沒有執行。 –