KING_JOKER6 2022-04-17 18:42 采纳率: 75%
浏览 180
已结题

想要在页面中设计button,点击之后显示下拉菜单,通过选择下拉菜单提供的数据读取到html

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

不知道怎么在button点击之后的页面里,出现下拉菜单并关联

问题相关代码,请勿粘贴截图

<!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">
          <script defer src="final.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
        <link rel="stylesheet" href="final.css">
    </head>

      <body onload="thirty_measurement()">
        <h2>Tier2 </h2>
        <button id="twenty_temp">Recent weather data</button>
        <div id="output"></div>
        <canvas id="weather_chart" width="500" height="300"> 
          <div id="DropDown"></div>
          
      </body>
      </html>

let chart1=null;
const setList=document.getElementById("DropDown");
document.getElementById('twenty_temp').addEventListener('click', twenty_temp);

function twenty_temp() {
    fetch("http://webapi19sa-1.course.tamk.cloud/v1/weather/temperature")
    .then((res) => res.json())
    .then((data) => {
        let output = '<h2>20 Temperature Data</h2>';
        let x_axis=new Array();
        let y_axis=new Array();
        for(let i = 0; i < 20; i++) {
            const weatherData = data[i];

            let seperate=JSON.stringify(weatherData.date_time);
            let arr = seperate.split("\"")[1];
//Here using split to devide array, as it has T at the end of date and time
            let sep_date = arr.split("T");
            let sep_time = sep_date[1].split("Z");
            x_axis[i]=sep_time[0];
            y_axis[i]=weatherData.temperature;
            output += `
    <table class="tableView2">
        <tr>
            <th>Row Number</th>
            <th>Measurement Date</th>
            <th>Measurement Time</th>
            <th>Measured temperature value</th>
        </tr>
        <tr>
            <td>${i + 1}</td>
            <td>${sep_date}</td>
            <td>${sep_time}</td>
            <td>${weatherData.temperature}</td>
        </tr>

        </table>
        `;
        }
        document.getElementById('output').innerHTML = output;
        chart_temp(x_axis,y_axis,data.length);
    })
}

function DropDown2(){
    setList.innerHTML=null;
    setList.innerHTML=`<select onchange="selectList2(this.value)">
    <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 selectList2(value){
    if(value==1){
        clickTage2("http://webapi19sa-1.course.tamk.cloud/v1/weather/temperature");
    }
    else if(value==2){
        clickTage2("http://webapi19sa-1.course.tamk.cloud/v1/weather/temperature/23");
    }
    else if(value==3){
        clickTage2("http://webapi19sa-1.course.tamk.cloud/v1/weather/temperature/47");
    }
    else if(value==4){
        clickTage2("http://webapi19sa-1.course.tamk.cloud/v1/weather/temperature/71");
    }
    else{
        clickTage2("http://webapi19sa-1.course.tamk.cloud/v1/weather/temperature/167");
    }
}

function chart_temp(sep_date,weatherData,period){
            
    const ctx=document.getElementById('weather_chart').getContext('2d');
    if(period==20){
        chartName="The Latest 20 Temperature Data";
    }
    else if(period==24){
        chartName="The Latest 24 Hourly Avg Temperature"
    }
    else if(period==48){
        chartName="The Latest 48 Hourly Avg Temperature"
    }
    else if(period==72){
        chartName="The Latest 72 Hourly Avg Temperature"
    }
    else{
        chartName="The Latest 1 Week Hourly Avg Temperature"
    }

    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:[]
            }
          });
        
}
运行结果及报错内容

无法运行

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

思路:原本的页面可以读取数据并传输到html,现在的想法是再点击button之后,里面能够出现新下拉菜单,并根据新下拉菜单选择的时间,读取不同的数据
框架已经构建好,我朋友用的是

  • 方法,设计超链接,但是如果我想用button方法,不知道如何实现下拉菜单,已经构建好了一个select构建下拉菜单的框架在html,但无法与function名字连接
    尝试方法:重新命名一个下拉菜单但无法实现

    我想要达到的结果

    能够根据新下拉菜单选择的时间,显示出不同的数据读取

  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2022-04-17 20:14
    关注

    clickTage2方法没没见定义,可以修改twenty_temp增一个url参数。感觉都不需要按钮,选择select直接加载数据就行,然后onchange调用twenty_temp函数加载数据。
    代码如下,不知道是否题主要的

    img

    
    <!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">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
        <link rel="stylesheet" href="final.css">
    </head>
    
    <body onload="thirty_measurement()">
        <h2>Tier2 </h2>
        <!--<button id="twenty_temp">Recent weather data</button>-->
        <div id="output"></div>
        <canvas id="weather_chart" width="500" height="300"></canvas>
        <div id="DropDown"></div>
        <script>
    
            let chart1 = null;
            const setList = document.getElementById("DropDown");
            //document.getElementById('twenty_temp').addEventListener('click', twenty_temp);
            DropDown2()//生成select
            function twenty_temp(url) {
                fetch(url)
                    .then((res) => res.json())
                    .then((data) => {
                        let output = '<h2>20 Temperature Data</h2>';
                        let x_axis = new Array();
                        let y_axis = new Array();
                        for (let i = 0; i < 20; i++) {
                            const weatherData = data[i];
    
                            let seperate = JSON.stringify(weatherData.date_time);
                            let arr = seperate.split("\"")[1];
                            //Here using split to devide array, as it has T at the end of date and time
                            let sep_date = arr.split("T");
                            let sep_time = sep_date[1].split("Z");
                            x_axis[i] = sep_time[0];
                            y_axis[i] = weatherData.temperature;
                            output += `
                <table class="tableView2">
                    <tr>
                        <th>Row Number</th>
                        <th>Measurement Date</th>
                        <th>Measurement Time</th>
                        <th>Measured temperature value</th>
                    </tr>
                    <tr>
                        <td>${i + 1}</td>
                        <td>${sep_date}</td>
                        <td>${sep_time}</td>
                        <td>${weatherData.temperature}</td>
                    </tr>
    
                    </table>
                    `;
                        }
                        document.getElementById('output').innerHTML = output;
                        chart_temp(x_axis, y_axis, data.length);
                    })
            }
    
            function DropDown2() {
                setList.innerHTML = null;
                setList.innerHTML = `<select onchange="selectList2(this.value)">
                <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 selectList2(value) {
                if (value == 1) {
                    twenty_temp("http://webapi19sa-1.course.tamk.cloud/v1/weather/temperature");
                }
                else if (value == 2) {
                    twenty_temp("http://webapi19sa-1.course.tamk.cloud/v1/weather/temperature/23");
                }
                else if (value == 3) {
                    twenty_temp("http://webapi19sa-1.course.tamk.cloud/v1/weather/temperature/47");
                }
                else if (value == 4) {
                    twenty_temp("http://webapi19sa-1.course.tamk.cloud/v1/weather/temperature/71");
                }
                else {
                    twenty_temp("http://webapi19sa-1.course.tamk.cloud/v1/weather/temperature/167");
                }
            }
    
            function chart_temp(sep_date, weatherData, period) {
    
                const ctx = document.getElementById('weather_chart').getContext('2d');
                if (period == 20) {
                    chartName = "The Latest 20 Temperature Data";
                }
                else if (period == 24) {
                    chartName = "The Latest 24 Hourly Avg Temperature"
                }
                else if (period == 48) {
                    chartName = "The Latest 48 Hourly Avg Temperature"
                }
                else if (period == 72) {
                    chartName = "The Latest 72 Hourly Avg Temperature"
                }
                else {
                    chartName = "The Latest 1 Week Hourly Avg Temperature"
                }
    
                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: []
                    }
                });
    
            }
        </script>
    </body>
    </html>
    

    img


    有其他问题可以继续交流~

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

报告相同问题?

问题事件

  • 系统已结题 4月25日
  • 已采纳回答 4月17日
  • 修改了问题 4月17日
  • 创建了问题 4月17日

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?