0

我有一个案例,我需要根据另一个 javascript 的输入加载一个字符。但它在我的情况下不起作用。我添加了以下代码:

 <script type="text/javascript">
    // Load the Visualization API and the piechart package.
    google.load('visualization', '1', {'packages':['corechart', 'table']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);

    function drawChart() {
      var json = $.ajax({
        url: fileURL, // make this url point to the data file
        dataType: 'json',
        cahce:false,
        async: false
      }).responseText;



      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(json);
      var options = {
        title: graphTitle,
        is3D: 'true',
        width: 800,
        height: 600
      };
      var tableOptions = {
       title: 'App Listing'
     };
      // Instantiate and draw our chart, passing in some options.

      var chart = new       google.visualization.PieChart(document.getElementById('chart_div'));
      chart.draw(data, options);




    }

    </script>  

我传递了 graphtitle 和 fileURL 的值,如下所示:

 <script type="text/javascript">
    $(document).ready(function () {
      var fileURL = "";
      var graphTitle = "";


      function showDiv() {
        if($firstCheck) {
          var selText;
          $("#dd4 li a").show(function () {
            selText = $(this).text();
          });

          if(selText !== "Factor"){
            if(selText == "IA Architecture Usage"){
              fileURL = "get_json.php";
              graphTitle = "IA Architecture Variation";
            }else if(selText ==  "Tablet to Phone"){
              fileURL = "get_tablet_support.php";
              graphTitle = "Tablet Usage Variation";

            }
          document.getElementById('chart_div').style.display = "block";
          }
        }else{
          document.getElementById('chart_div').style.display = "none";
        }



    }

 </script>

这两个 javascript 都在同一个文件中。使用上述代码时,我无法传递 fileURL 和 graphTitle。知道如何解决这个问题吗?

4

2 回答 2

1

Use global variables with window. E.g.

$(document).ready(function () {
window.fileURL = "";
window.graphTitle = "";
});

Don't specify "var" or it will only be within the scope of the function.

EDIT: Also make sure that the script in which your variables are assigned initially is before the other one.

于 2013-11-14T02:30:19.847 回答
0

更面向 OO 的东西(不是真正的 OO,而是更少的inline代码)怎么样?它更清洁、更易于阅读/维护..example 仍然可以使用一些工作,但我相信你明白了。

function loadChart(title, url) {
    // Load the Visualization API and the piechart package.
    google.load('visualization', '1', {'packages':['corechart', 'table']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);

    function drawChart() {
      var json = $.ajax({
        url     : url, // make this url point to the data file
        dataType: 'json',
        cahce   : false,
        async   : false
      });



      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(json);
      var options = {
        title : title,
        is3D  : 'true',
        width : 800,
        height: 600
      };

      var tableOptions = {
       title: 'App Listing'
     };
      // Instantiate and draw our chart, passing in some options.

      var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    }
}

$(document).ready(function () {
    var fileURL    = "";
    var graphTitle = "";

    function showDiv() {
        if($firstCheck) {
          var selText;

          $("#dd4 li a").show(function () {
            selText = $(this).text();
          });

          if(selText !== "Factor") {
            if(selText == "IA Architecture Usage"){
              fileURL    = "get_json.php";
              graphTitle = "IA Architecture Variation";

            } else if(selText ==  "Tablet to Phone"){
              fileURL    = "get_tablet_support.php";
              graphTitle = "Tablet Usage Variation";
            }

            document.getElementById('chart_div').style.display = "block";
          }

        } else {
          document.getElementById('chart_div').style.display = "none";
        }

        loadChart(graphTitle, fileURL);
    }
}

顺便说一句,我认为您的代码中有错误:.responseText对我来说似乎毫无用处,而且很可能本身会引发错误。我也不知道谁showDiv()在代码中调用。从示例中,我会说它永远不会触发。

于 2013-11-14T02:46:38.807 回答