0

我遇到一个问题,json jquery 调用仅在页面加载后刷新页面时起作用。即在页面加载时不刷新数据,在刷新页面时刷新数据。它提供一个选择列表。我有以下 jquery ajax 设置:

$(document).ready(function() { 
    $.ajaxSetup({'async': false, 'cache':false});

以下是我的 json 声明:

    $.getJSON('ajax.cfm?action=refresh_bsu&site='+site+'&BaseLineYear='+BaselineYear+'&item=EnergyData', function(EnergyData) {
        SiteEnergyData = EnergyData; //Make it a global variable        
    }); 

这是完整的功能:

function GetEnergyData(){
    var site = $("#busUnitSubGroup").val();
    var BaselineYear = $("#BaselineYear").val();

    SiteEnergyData = "";

    $.getJSON('ajax.cfm?action=refresh_bsu&site='+site+'&BaseLineYear='+BaselineYear+'&item=EnergyData', function(EnergyData) {
        SiteEnergyData = EnergyData; //Make it a global variable        
    }); 
}

任何人都可以帮我解决这个问题吗?

谢谢JC

4

2 回答 2

0

I believe you might need define the function outside of doc ready, so that it is defined by the time the doc.ready event fires:

$(document).ready(function(){   

    GetEnergyData();          

    ...

});

function GetEnergyData(){
  ....
}

However, it seems like you should be getting some javascript errors if that is the problem. You should check the F12->console to see if there are any errors and what line it is saying the error is on.

于 2013-03-29T22:20:22.290 回答
0

(a)SiteEnergyData在函数外部定义GetEnergyData()(因为您已经注意到它是一个全局变量)。如果没有,它将仅在该函数中可见。

(b) 你GetEnergyData()准备好在文档中的某个地方打电话了吗?我假设是这样,因为(出于某种原因)它适用于重新加载但不适用于初始加载。

$(document).ready(function(){

    var SiteEnergyData = "";  // (a) global variable... well, within doc ready
    GetEnergyData();          // (b) calls function once page is loaded

    ...

    function GetEnergyData(){
        ....
    }
});
于 2013-03-28T23:03:49.167 回答