2012-12-23 31 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中的代码根本没有执行。 –