如何使用javascript将表单输出链接到google fusion表查询

时间:2022-10-16 13:56:11

I am trying to create a html form which will display a google fusion table depending on the users inputted county.

我正在尝试创建一个html表单,它将根据用户输入的县显示谷歌融合表。

Here is the code I currently have which displays the results for county louth.

这是我目前拥有的代码,用于显示郡劳斯的结果。

<title>Sample form</title>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
    google.charts.load("current", { packages: ['table'] });
    google.charts.setOnLoadCallback(drawVisualization);
    function drawVisualization() {
        google.visualization.drawChart({
            "containerId": "visualization_div",
            "dataSourceUrl": "//www.google.com/fusiontables/gvizdata?tq=",
            "query": "SELECT 'AIRO_ID', 'Off_Name', 'County'FROM " +
                    "1BeYE5fGPxo3yTNdmL_JE63JEMANnckYwcUmW4ouV WHERE 'County' = 'Louth'",
            "refreshInterval": 5,
            "chartType": "Table",
            "options": {}
        });
    }
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<div id="visualization_div" style="width: 600px; height: 400px;"></div>
</body>

I would like to use the output of the following form to be used instead of louth. But I cant seem to figure out how to link the form to the javascript.

我想使用以下形式的输出来代替louth。但我似乎无法弄清楚如何将表单链接到JavaScript。

<html>    <form>
    <select name="County">
        <option value="Antrim">Antrim</option>
        <option value="Armagh">Armagh</option>
        <option value="Carlow">Carlow</option>
        <option value="Cavan">Cavan</option>
        <option value="Clare">Clare</option>
        <option value="Cork">Cork</option>
        <option value="Derry">Derry</option>
        <option value="Donegal">Donegal</option>
        <option value="Down">Down</option>
        <option value="Dublin">Dublin</option>
        <option value="Fermanagh">Fermanagh</option>
        <option value="Galway">Galway</option>
        <option value="Kerry">Kerry</option>
        <option value="Kildare">Kildare</option>
        <option value="Kilkenny">Kilkenny</option>
        <option value="Laois">Laois</option>
        <option value="Leitrim">Leitrim</option>
        <option value="Limerick">Limerick</option>
        <option value="Longford">Longford</option>
        <option value="Louth">Louth</option>
        <option value="Mayo">Mayo</option>
        <option value="Meath">Meath</option>
        <option value="Monaghan">Monaghan</option>
        <option value="Offaly">Offaly</option>
        <option value="Roscommon">Roscommon</option>
        <option value="Sligo">Sligo</option>
        <option value="Tipperary">Tipperary</option>
        <option value="Tyrone">Tyrone</option>
        <option value="Waterford">Waterford</option>
        <option value="Westmeath">Westmeath</option>
        <option value="Wexford">Wexford</option>
        <option value="Wicklow">Wicklow</option>
    </select>
    <input type="submit" value="Submit">

1 个解决方案

#1


1  

One way to link the <select> element with the Javascript is to use the onchange event. Then, whenever a new element is selected in the dropdown menu, a function (showCounty() in this case) is called. This function updates the chart using the selected value.

Notice that the onchange event takes a function as its argument, and the function is passed the event data (which includes the selected value) using the this keyword.

请注意,onchange事件将函数作为其参数,并使用this关键字向函数传递事件数据(包括所选值)。

Here's a modification to your code that shows this technique:

这是对代码的修改,显示了这种技术:

google.charts.load("current", { packages: ['table'] });
google.charts.setOnLoadCallback(loadDefault);

function showCounty(event) {
    google.visualization.drawChart({
      "containerId": "visualization_div",
      "dataSourceUrl": "//www.google.com/fusiontables/gvizdata?tq=",
      "query": "SELECT 'AIRO_ID', 'Off_Name', 'County'FROM " +
               "1BeYE5fGPxo3yTNdmL_JE63JEMANnckYwcUmW4ouV WHERE 'County'='" 
               + event.value + "'",
      "chartType": "Table",
      "options": {}
    });
}

function loadDefault() {
  showCounty({value: "Louth"});
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="visualization_div" style="width: 600px; height: 180px;"></div>
<select name="County" onchange="showCounty(this)">
  <option value="Cork">Cork</option>
  <option value="Dublin">Dublin</option>
  <option value="Louth" selected="selected">Louth</option>
</select>

#1


1  

One way to link the <select> element with the Javascript is to use the onchange event. Then, whenever a new element is selected in the dropdown menu, a function (showCounty() in this case) is called. This function updates the chart using the selected value.

Notice that the onchange event takes a function as its argument, and the function is passed the event data (which includes the selected value) using the this keyword.

请注意,onchange事件将函数作为其参数,并使用this关键字向函数传递事件数据(包括所选值)。

Here's a modification to your code that shows this technique:

这是对代码的修改,显示了这种技术:

google.charts.load("current", { packages: ['table'] });
google.charts.setOnLoadCallback(loadDefault);

function showCounty(event) {
    google.visualization.drawChart({
      "containerId": "visualization_div",
      "dataSourceUrl": "//www.google.com/fusiontables/gvizdata?tq=",
      "query": "SELECT 'AIRO_ID', 'Off_Name', 'County'FROM " +
               "1BeYE5fGPxo3yTNdmL_JE63JEMANnckYwcUmW4ouV WHERE 'County'='" 
               + event.value + "'",
      "chartType": "Table",
      "options": {}
    });
}

function loadDefault() {
  showCounty({value: "Louth"});
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="visualization_div" style="width: 600px; height: 180px;"></div>
<select name="County" onchange="showCounty(this)">
  <option value="Cork">Cork</option>
  <option value="Dublin">Dublin</option>
  <option value="Louth" selected="selected">Louth</option>
</select>