如何从文本框更改javascript值

时间:2015-08-10 12:16:55

标签: javascript

我有两页1)数据2)饼图 在数据页面中,我有5个文本框和一个按钮。

我想在点击按钮后更改文本框值中的以下值

['Work',     11],
['Eat',      2],
['Commute',  2],
['Watch TV', 2],
['Sleep',    7]

怎么做?

这是我的代码:

<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['Task', 'Hours per Day'],
      ['Work',     11],
      ['Eat',      2],
      ['Commute',  2],
      ['Watch TV', 2],
      ['Sleep',    7]
    ]);

    var options = {
      title: 'My Daily Activities'
    };
    var chart = new google.visualization.PieChart(document.getElementById('piechart'));

    chart.draw(data, options);
  }
</script>
</head>
<body>
<div id="piechart" style="width: 900px; height: 500px;"></div>
</body>

1 个答案:

答案 0 :(得分:0)

您可以重复使用drawChart()代码并在事件clickonsubmit等内部使用它(也可以在加载时动态加载)

** 请注意,您可以使用Ajax或只是从html inputs获取一些值来重绘饼图。

下面我刚刚演示了如何实现,你需要根据自己的需要进行修改。

&#13;
&#13;
google.load("visualization", "1", {
  packages: ["corechart"]
});
google.setOnLoadCallback(drawChart);

function drawChart() {

  var data = google.visualization.arrayToDataTable([
    ['Task', 'Hours per Day'],
    ['Work', 11],
    ['Eat', 2],
    ['Commute', 2],
    ['Watch TV', 2],
    ['Sleep', 7]
  ]);

  var options = {
    title: 'My Daily Activities'
  };
  var chart = new google.visualization.PieChart(document.getElementById('piechart'));

  chart.draw(data, options);
}

$(function() {
  $('#ctc').on('click', function() {
    var data = google.visualization.arrayToDataTable([
      ['Task', 'Hours per Day'],
      ['Work', 20],
      ['Eat', 12],
      ['Commute', 12],
      ['Watch TV', 2],
      ['Sleep', 7]
    ]);

    var options = {
      title: 'My Daily Activities - Now changed on click !!'
    };

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

  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<h2 id='ctc'>Click me</h2>
<div id="piechart" style="width: 900px; height: 500px;"></div>
&#13;
&#13;
&#13;