简体   繁体   English

select 标记 onchange 未执行,function 失败:未捕获的 ReferenceError:...未定义

[英]select tag onchange does not execute, function failure: Uncaught ReferenceError: ... is not defined


I'm pretty new to js. 我对js很陌生。
I want to execute the function buildChart when the select value changes. 当 select 值发生变化时,我想执行 function buildChart。

But I get the following error all the time. 但我一直收到以下错误。
 Uncaught ReferenceError: buildChart is not defined

Does anybody have an idea how to solve that?有人知道如何解决这个问题吗?
Thank you very much in advance!非常感谢您! You would be a great help!!你会是一个很大的帮助!

My JS function is as follows:我的 JS function 如下:

 class GetData { getCanvasUrl(id) { return document.getElementById(id).getAttribute("data-url") } getDropdownValue() { return document.getElementById("dropdown-pp").value; } async fetchData(url) { const response = await fetch(url); return await response.json(); } } const getData = new GetData(); function buildChart() { let dropdownValue = getData.getDropdownValue(); if (dropdownValue === "All Generations") { console.log("All Generations") } else { console.log("other") } } buildChart();

My HTML follows here:我的 HTML 如下:

 {% extends "base.html"%} {% load static %} {% block head %} <script rel="script" type="module" src="{% static 'js/productionplan/productionplan.js' %}" defer></script> {% endblock head %} {% block body %} <select id="dropdown-pp" onchange="buildChart();" class="form-select" aria-label="Default select example"> {% endblock %}

Thanks to Teemu I was able to solve the problem.感谢Teemu,我能够解决这个问题。
The magic is in the last line.魔术在最后一行。

Here is my code:这是我的代码:

import  { stackedBar } from '../charts.js'

stackedBar("#bar-chart");


class GetData {

    getCanvasUrl(id) {
        return document.getElementById(id).getAttribute("data-url")
    }

    getDropdownValue() {
        return document.getElementById("dropdown-pp").value;
    }

    async fetchData(url) {
        const response = await fetch(url);
        return await response.json();
    }
}

const getData = new GetData();

function buildChart() {
    let dropdownValue = getData.getDropdownValue();
    if (dropdownValue === "All Generations") {
        console.log("All Generations")
    } else {
        console.log("other")
    }
}

document.getElementById("dropdown-pp").addEventListener("change", buildChart);

When the select tag with dropdown-pp changes the function buildChart is called.当带有 dropdown-pp 的 select 标记更改时,将调用 function buildChart。

Thanks a lot Teemu!!!!!!!!!!!非常感谢提姆!!!!!!!!!!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM