KING_JOKER6 2022-04-22 20:47 采纳率: 75%
浏览 51
已结题

从数据库读取数据,并有两个下拉菜单具体选择后呈现

问题遇到的现象和发生背景

无法把两个下拉菜单呈现出来,只能呈现一个

问题相关代码,请勿粘贴截图
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Final Assigment</title>
    <script type="text/javascript"></script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="final.css">
</head>
 
<body onload="thirty_measurement()">
    <h2>Tier4 </h2>
    <button class="btn" id="free_choice">Free Choice</button>
    <div id="DropDown"></div>
    <div id="output"></div>
    <canvas id="weather_chart" width="500" height="300"></canvas>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
    <script defer src="final.js"></script>
</body>
</html>

let chart1 = null;
const setList = document.getElementById("DropDown");

document.getElementById('free_choice').addEventListener('click', DropDown4);

    function DropDown4(type) {
        setList.innerHTML = null;
        setList.innerHTML = `<select onchange="selectList4(this.value),'${type}'">
     <option selected>Please select a time period</option>
     <option value="1">Rain</option>
     <option value="2">Wind Speed</option>
     <option value="3">Wind Direction</option>
     <option value="4">Light</option>
     <option value="5">Temperature</option>
     </select>
     `
    }

    function DropDown5(type) {
        setList.innerHTML = null;
        setList.innerHTML = `<select onchange="selectList5(this.value),'${type}'">
     <option selected>Please select a time period</option>
     <option value="1">Now</option>
     <option value="2">24 hours</option>
     <option value="3">48 hours</option>
     <option value="4">72 hours</option>
     <option value="5">One week</option>
     </select>
     `
    }
 
    function selectList4(value,type) {
        if(type ==1){
        if (value == 1) {
            free_choice25_rain("http://webapi19sa-1.course.tamk.cloud/v1/weather/rain");
         }
        else if (value == 2) {
            free_choice1("http://webapi19sa-1.course.tamk.cloud/v1/weather/rain/23");
        }
        else if (value == 3) {
            free_choice1("http://webapi19sa-1.course.tamk.cloud/v1/weather/rain/47");
        }
        else if (value == 4) {
            free_choice1("http://webapi19sa-1.course.tamk.cloud/v1/weather/rain/71");
        }
        else {
            free_choice1("http://webapi19sa-1.course.tamk.cloud/v1/weather/rain/167");
        }
      }
      if(type ==2){
        if (value == 1) {
            free_choice25_wind_speed("http://webapi19sa-1.course.tamk.cloud/v1/weather/wind_speed");
         }
        else if (value == 2) {
            free_choice2("http://webapi19sa-1.course.tamk.cloud/v1/weather/wind_speed/23");
        }
        else if (value == 3) {
            free_choice2("http://webapi19sa-1.course.tamk.cloud/v1/weather/wind_speed/47");
        }
        else if (value == 4) {
            free_choice2("http://webapi19sa-1.course.tamk.cloud/v1/weather/wind_speed/71");
        }
        else {
            free_choice2("http://webapi19sa-1.course.tamk.cloud/v1/weather/wind_speed/167");
        }
      }
      if(type ==3){
        if (value == 1) {
            free_choice25_wind_direction("http://webapi19sa-1.course.tamk.cloud/v1/weather/wind_direction");
         }
        else if (value == 2) {
            free_choice3("http://webapi19sa-1.course.tamk.cloud/v1/weather/wind_direction/23");
        }
        else if (value == 3) {
            free_choice3("http://webapi19sa-1.course.tamk.cloud/v1/weather/wind_direction/47");
        }
        else if (value == 4) {
            free_choice3("http://webapi19sa-1.course.tamk.cloud/v1/weather/wind_direction/71");
        }
        else {
            free_choice3("http://webapi19sa-1.course.tamk.cloud/v1/weather/wind_direction/167");
        }
      }
    function free_choice25_rain(url) {
        fetch(url)
            .then((res) => res.json())
            .then((data) => {
                let output = `<h2>25 Free Choice Data</h2>
                <table class="tableView2">
                <tr>
                    <th>Row Number</th>
                    <th>Measurement Date</th>
                    <th>Measurement Time</th>
                    <th>Measured value</th>
                </tr>
                `;
            
                let x_axis = new Array();
                let y_axis = new Array();
                for (let i = 0; i < 25; i++) {
                    const weatherData = data[i];

                    let arr = weatherData.date_time.split("T");
                    //Here using split to devide array, as it has T at the end of date and time
                    let sep_date = arr[0];
                    let sep_time = arr[1];

                    x_axis[i] = sep_time;
                    y_axis[i] = weatherData.rain;
                    output += `
        <tr>
            <td>${i + 1}</td>
            <td>${sep_date}</td>
            <td>${sep_time}</td>
            <td>${weatherData.rain}</td>
        </tr>
        `;
                    }
                    output+='</table>'
                    document.getElementById('output').innerHTML = output;
                    chart_25rain(x_axis, y_axis, data.length);
                })
    }

    function chart_25rain(sep_date, weatherData, period) {
 
        const ctx = document.getElementById('weather_chart').getContext('2d');
        chartName = "The Latest 25 Rain Data";

        if (chart1 != null) {
            chart1.destroy();
        }
        chart1 = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: sep_date,
                datasets: [{
                    data: weatherData,
                    backgroundColor: 'reba(127,255,127,0.4)',
                    borderWidth: 3,
                    }]
                },
                options: {
                    legend: {
                        display: false
                    },
                    scales: {
                        yAxes: [{
                            ticks: {
                                beginAtZero: true
                            }
                        }]
                    },
                    title: {
                        display: true,
                        text: chartName,
                    },
                    animation: false,
                    events: []
                }
            });
 
    }

    function free_choice25_wind_speed(url) {
        fetch(url)
            .then((res) => res.json())
            .then((data) => {
                let output = `<h2>25 Free Choice Data</h2>
                <table class="tableView2">
                <tr>
                    <th>Row Number</th>
                    <th>Measurement Date</th>
                    <th>Measurement Time</th>
                    <th>Measured value</th>
                </tr>
                `;
            
                let x_axis = new Array();
                let y_axis = new Array();
                for (let i = 0; i < 25; i++) {
                    const weatherData = data[i];

                    let arr = weatherData.date_time.split("T");
                    //Here using split to devide array, as it has T at the end of date and time
                    let sep_date = arr[0];
                    let sep_time = arr[1];

                    x_axis[i] = sep_time;
                    y_axis[i] = weatherData.wind_speed;
                    output += `
        <tr>
            <td>${i + 1}</td>
            <td>${sep_date}</td>
            <td>${sep_time}</td>
            <td>${weatherData.wind_speed}</td>
        </tr>
        `;
                    }
                    output+='</table>'
                    document.getElementById('output').innerHTML = output;
                    chart_25wind_speed(x_axis, y_axis, data.length);
                })
    }

    function chart_25wind_speed(sep_date, weatherData, period) {
 
        const ctx = document.getElementById('weather_chart').getContext('2d');
        chartName = "The Latest 25 Wind Speed Data";
        
        if (chart1 != null) {
            chart1.destroy();
        }
        chart1 = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: sep_date,
                datasets: [{
                    data: weatherData,
                    backgroundColor: 'reba(127,255,127,0.4)',
                    borderWidth: 3,
                    }]
                },
                options: {
                    legend: {
                        display: false
                    },
                    scales: {
                        yAxes: [{
                            ticks: {
                                beginAtZero: true
                            }
                        }]
                    },
                    title: {
                        display: true,
                        text: chartName,
                    },
                    animation: false,
                    events: []
                }
            });
 
    }

    function free_choice25_wind_direction(url) {
        fetch(url)
            .then((res) => res.json())
            .then((data) => {
                let output = `<h2>25 Free Choice Data</h2>
                <table class="tableView2">
                <tr>
                    <th>Row Number</th>
                    <th>Measurement Date</th>
                    <th>Measurement Time</th>
                    <th>Measured value</th>
                </tr>
                `;
            
                let x_axis = new Array();
                let y_axis = new Array();
                for (let i = 0; i < 25; i++) {
                    const weatherData = data[i];

                    let arr = weatherData.date_time.split("T");
                    //Here using split to devide array, as it has T at the end of date and time
                    let sep_date = arr[0];
                    let sep_time = arr[1];

                    x_axis[i] = sep_time;
                    y_axis[i] = weatherData.wind_direction;
                    output += `
        <tr>
            <td>${i + 1}</td>
            <td>${sep_date}</td>
            <td>${sep_time}</td>
            <td>${weatherData.wind_direction}</td>
        </tr>
        `;
                    }
                    output+='</table>'
                    document.getElementById('output').innerHTML = output;
                    chart_25wind_direction(x_axis, y_axis, data.length);
                })
    }

    function chart_25wind_direction(sep_date, weatherData, period) {
 
        const ctx = document.getElementById('weather_chart').getContext('2d');
        chartName = "The Latest 25 Wind Direction Data";
        
        if (chart1 != null) {
            chart1.destroy();
        }
        chart1 = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: sep_date,
                datasets: [{
                    data: weatherData,
                    backgroundColor: 'reba(127,255,127,0.4)',
                    borderWidth: 3,
                    }]
                },
                options: {
                    legend: {
                        display: false
                    },
                    scales: {
                        yAxes: [{
                            ticks: {
                                beginAtZero: true
                            }
                        }]
                    },
                    title: {
                        display: true,
                        text: chartName,
                    },
                    animation: false,
                    events: []
                }
            });
 
    }
function free_choice1(url) {
        fetch(url)
            .then((res) => res.json())
            .then((data) => {
                let output = `<h2>Rain Data</h2>
                <table class="tableView2">
                <tr>
                    <th>Row Number</th>
                    <th>Measurement Date</th>
                    <th>Measurement Time</th>
                    <th>Measured Rain value</th>
                </tr>
                `;
                let x_axis = new Array();
                let y_axis = new Array();
                for (let i = 0; i < data.length; i++) {
                    const weatherData = data[i];

                    let arr = weatherData.date_time.split("T");
                    //Here using split to devide array, as it has T at the end of date and time
                    let sep_date = arr[0];
                    let sep_time = arr[1];
                    x_axis[i] = sep_time;
                    y_axis[i] = weatherData.rain;
                    output += `
        <tr>
            <td>${i + 1}</td>
            <td>${sep_date}</td>
            <td>${sep_time}</td>
            <td>${weatherData.rain}</td>
        </tr>
        `;
                    }
                    output+='</table>'
                    document.getElementById('output').innerHTML = output;
                    chart_rain(x_axis, y_axis, data.length);
                })
    }
 
    function chart_rain(sep_date, weatherData, period) {
 
        const ctx = document.getElementById('weather_chart').getContext('2d');
        if (period == 25) {
            chartName = "The Latest 25 Rain Data";
        }
        else if (period == 24) {
            chartName = "The Latest 24 Hourly Avg Rain Data"
        }
        else if (period == 48) {
            chartName = "The Latest 48 Hourly Avg Rain Data"
        }
        else if (period == 72) {
            chartName = "The Latest 72 Hourly Avg Rain Data"
        }
        else {
             chartName = "The Latest 1 Week Hourly Avg Rain Data"
        }
 
        if (chart1 != null) {
            chart1.destroy();
        }
        chart1 = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: sep_date,
                datasets: [{
                    data: weatherData,
                    backgroundColor: 'reba(127,255,127,0.4)',
                    borderWidth: 3,
                    }]
                },
                options: {
                    legend: {
                        display: false
                    },
                    scales: {
                        yAxes: [{
                            ticks: {
                                beginAtZero: true
                            }
                        }]
                    },
                    title: {
                        display: true,
                        text: chartName,
                    },
                    animation: false,
                    events: []
                }
            });
 
    }

    function free_choice2(url) {
        fetch(url)
            .then((res) => res.json())
            .then((data) => {
                let output = `<h2>20 Wind Speed Data</h2>
                <table class="tableView2">
                <tr>
                    <th>Row Number</th>
                    <th>Measurement Date</th>
                    <th>Measurement Time</th>
                    <th>Measured Wind Speed value</th>
                </tr>
                `;
                let x_axis = new Array();
                let y_axis = new Array();
                for (let i = 0; i < data.length; i++) {
                    const weatherData = data[i];

                    let arr = weatherData.date_time.split("T");
                    //Here using split to devide array, as it has T at the end of date and time
                    let sep_date = arr[0];
                    let sep_time = arr[1];
                    x_axis[i] = sep_time;
                    y_axis[i] = weatherData.wind_speed;
                    output += `
        <tr>
            <td>${i + 1}</td>
            <td>${sep_date}</td>
            <td>${sep_time}</td>
            <td>${weatherData.wind_speed}</td>
        </tr>
        `;
                    }
                    output+='</table>'
                    document.getElementById('output').innerHTML = output;
                    chart_windspeed(x_axis, y_axis, data.length);
                })
    }
 
    function chart_windspeed(sep_date, weatherData, period) {
 
        const ctx = document.getElementById('weather_chart').getContext('2d');
        if (period == 25) {
            chartName = "The Latest 25 Wind Speed Data";
        }
        else if (period == 24) {
            chartName = "The Latest 24 Hourly Avg Wind Speed Data"
        }
        else if (period == 48) {
            chartName = "The Latest 48 Hourly Avg Wind Speed Data"
        }
        else if (period == 72) {
            chartName = "The Latest 72 Hourly Avg Wind Speed Data"
        }
        else {
             chartName = "The Latest 1 Week Hourly Avg Wind Speed Data"
        }
 
        if (chart1 != null) {
            chart1.destroy();
        }
        chart1 = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: sep_date,
                datasets: [{
                    data: weatherData,
                    backgroundColor: 'reba(127,255,127,0.4)',
                    borderWidth: 3,
                    }]
                },
                options: {
                    legend: {
                        display: false
                    },
                    scales: {
                        yAxes: [{
                            ticks: {
                                beginAtZero: true
                            }
                        }]
                    },
                    title: {
                        display: true,
                        text: chartName,
                    },
                    animation: false,
                    events: []
                }
            });
 
    }

function free_choice3(url) {
        fetch(url)
            .then((res) => res.json())
            .then((data) => {
                let output = `<h2>20 Wind Direction Data</h2>
                <table class="tableView2">
                <tr>
                    <th>Row Number</th>
                    <th>Measurement Date</th>
                    <th>Measurement Time</th>
                    <th>Measured Wind Direction value</th>
                </tr>
                `;
                let x_axis = new Array();
                let y_axis = new Array();
                for (let i = 0; i < data.length; i++) {
                    const weatherData = data[i];

                    let arr = weatherData.date_time.split("T");
                    //Here using split to devide array, as it has T at the end of date and time
                    let sep_date = arr[0];
                    let sep_time = arr[1];
                    x_axis[i] = sep_time;
                    y_axis[i] = weatherData.wind_direction;
                    output += `
        <tr>
            <td>${i + 1}</td>
            <td>${sep_date}</td>
            <td>${sep_time}</td>
            <td>${weatherData.wind_direction}</td>
        </tr>
        `;
                    }
                    output+='</table>'
                    document.getElementById('output').innerHTML = output;
                    chart_winddirection(x_axis, y_axis, data.length);
                })
    }
 
    function chart_winddirection(sep_date, weatherData, period) {
 
        const ctx = document.getElementById('weather_chart').getContext('2d');
        if (period == 25) {
            chartName = "The Latest 25 Wind Direction Data";
        }
        else if (period == 24) {
            chartName = "The Latest 24 Hourly Avg Wind Direction Data"
        }
        else if (period == 48) {
            chartName = "The Latest 48 Hourly Avg Wind Direction Data"
        }
        else if (period == 72) {
            chartName = "The Latest 72 Hourly Avg Wind Direction Data"
        }
        else {
             chartName = "The Latest 1 Week Hourly Avg Wind Direction Data"
        }
 
        if (chart1 != null) {
            chart1.destroy();
        }
        chart1 = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: sep_date,
                datasets: [{
                    data: weatherData,
                    backgroundColor: 'reba(127,255,127,0.4)',
                    borderWidth: 3,
                    }]
                },
                options: {
                    legend: {
                        display: false
                    },
                    scales: {
                        yAxes: [{
                            ticks: {
                                beginAtZero: true
                            }
                        }]
                    },
                    title: {
                        display: true,
                        text: chartName,
                    },
                    animation: false,
                    events: []
                }
            });
 
    }

运行结果及报错内容

运行后只显示第一个下拉菜单,

我的解答思路和尝试过的方法

尝试使用

  • ,但这是不同方法,想纯粹用select做出两个下拉菜单,
    无法顺利连接两个下拉菜单与功能

    我想要达到的结果

    想要点击Free Choice以后,出现两个下拉菜单,根据两个下拉菜单选择的内容,呈现不同数据和图表

  • 写回答

3条回答 默认 最新

  • 关注

    你free_choice按钮只绑定了DropDown4一个函数自然只生成一个下拉菜单
    要绑定DropDown4和DropDown5两个函数

    document.getElementById('free_choice').addEventListener('click', function () {
    DropDown4(123); //123是要传递的type
    DropDown5(456); //456是要传递的type
    });

    并且要在不同的div中生成下拉菜单

    <div id="DropDown1"></div>
    <div id="DropDown2"></div>
    

    代码如下

        <h2>Tier4 </h2>
        <button class="btn" id="free_choice">Free Choice</button>
        <div id="DropDown1"></div>
        <div id="DropDown2"></div>
        <div id="output"></div>
    

    js

    let chart1 = null;
    const setList1 = document.getElementById("DropDown1");
    const setList2 = document.getElementById("DropDown2");
    document.getElementById('free_choice').addEventListener('click', function () {
        DropDown4(123); //123是要传递的type
        DropDown5(456); //456是要传递的type
    }); 
        function DropDown4(type) {
            setList1.innerHTML = `<select onchange="selectList4(this.value,'${type}')">
         <option selected>Please select a time period</option>
         <option value="1">Rain</option>
         <option value="2">Wind Speed</option>
         <option value="3">Wind Direction</option>
         <option value="4">Light</option>
         <option value="5">Temperature</option>
         </select>
         `
        }
     
        function DropDown5(type) {
            setList2.innerHTML = `<select onchange="selectList5(this.value,'${type}')">
         <option selected>Please select a time period</option>
         <option value="1">Now</option>
         <option value="2">24 hours</option>
         <option value="3">48 hours</option>
         <option value="4">72 hours</option>
         <option value="5">One week</option>
         </select>
         `
        }
    

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月10日
  • 已采纳回答 5月2日
  • 创建了问题 4月22日

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备