3

我已经去掉了我的页面上所有的花里胡哨,试图让它工作——我有 5 个按钮和一个 DIV。该页面从谷歌电子表格中读取数据行并填充数组 data[]。

<html>
<head>

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">

  google.load('visualization', '1', {'packages': ['table']});
  var i, j, x, k;
  var data = new Array();
  var headers = new Array();
  var numRows, numCols;

  function onldQuery() {

    var url = 'http://spreadsheets.google.com/tq?key=xxxxxx&pub=0', query = new google.visualization.Query(url); 
    query.setQuery('SELECT * WHERE B != "" AND V> 0 ORDER BY V DESC LIMIT 20');
    query.send(handleOnldResponse);
  }

  function handleOnldResponse(response) {
    if (response.isError()) {
      alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());

      return;
    }

    numRows = response.getDataTable().getNumberOfRows();
    numCols = response.getDataTable().getNumberOfColumns();

    console.log(numRows);
    for (i = 0; i < numRows; i++) {
      for (j = 0; j < numCols; j++) {
        headers[j] = response.getDataTable().getColumnLabel(j);
        data[i,j] = response.getDataTable().getValue(i, j);
        console.log(data[i,j]);
      }
    }

    loadData(0);
  }

  function loadData(x) {
    console.log(x);
    console.log(data[x,1]);
    console.log(numCols);

    $('#test_card').html("");

    for (k = 0; k < numCols;k++) {
      $('#test_card').html($('#test_card').html() + data[x,k] + "<br>");
    }
  }

</script>
</head>

<body onload="onldQuery()">
<button id="btn1" onclick="loadData(1);">1</button>
<button id="btn2" onclick="loadData(2);">2</button>
<button id="btn3" onclick="loadData(3);">3</button>
<button id="btn4" onclick="loadData(4);">4</button>
<button id="btn5" onclick="loadData(5);">5</button>

<div id="test_card">

</div>

</body>
</html>

它根据控制台转储将数据正确加载到data[]数组中,但我无法弄清楚为什么它只会在 a:加载页面和 b:尝试将不同的行加载到DIV 通过单击其中一个按钮。

4

1 回答 1

2

尝试使用 .append()

$('#test_card').append(data[x,k] + "<br>");
于 2013-04-18T15:39:20.280 回答