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

想要在页面中设计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条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

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

      悬赏问题

      • ¥15 Multisim14.0软件安装
      • ¥15 一块GD32F105芯片的主板。怎么找RXD和TXD串口
      • ¥15 磁盘异常导致工业相机GigE传图卡顿
      • ¥20 python中使用chatgpt为什么一直连接失败
      • ¥50 使用grpc遇到的问题
      • ¥100 运算速度优化问题,控制在四秒以内
      • ¥15 如何用matlab画出这样的图
      • ¥15 线性回归问题进行特征缩放后,为什么求得的参数不对了
      • ¥15 python爬取网页信息(主要为期刊影响因子、官网等),已有ISSN进行检索,但爬取过程找不到class该怎么办?
      • ¥15 找出5阶幻方的所有解的个数:275305224