简体   繁体   English

JavaScript function 未在 Django 中定义

[英]JavaScript function is not defined in Django

I have a small Django project.我有一个小的 Django 项目。 I have a JS script in the HTML file for one of my apps:我的一个应用程序的 HTML 文件中有一个 JS 脚本:

<script type="text/javascript">
    function intervalSelector() {
        let intervalField = document.getElementById("intervalField");
        let hourField = document.getElementById("hourField");
        let minuteField = document.getElementById("minuteField");

        intervalField.addEventListener("change", function () {

            if (intervalField.value === "Hourly") {
                hourField.disabled = true;
                minuteField.disabled = false;
            } else {
                hourField.disabled = false;
                minuteField.disabled = false;
            }
        });
    }
</script>

This function is called by a button element further down in the HTML document:此 function 由 HTML 文档中更下方的按钮元素调用:

<button style="background-color: orange; color: white; float:left; margin-right:5px;"
        class="btn" type="button"
        onclick="intervalSelector()"
        data-bs-toggle="collapse"
        data-bs-target="#collapseWidthExample" aria-expanded="false"
        aria-controls="collapseWidthExample">
        Schedule
</button>

The function gets three elements by their Id in the HTML document: function 通过它们在 HTML 文档中的 Id 获取三个元素:

<select class="form-select form-select-lg mb-3"
        aria-label=".form-select-lg example" id="intervalField">
        <option selected>Interval</option>
        <option value="1">Daily</option>
        <option value="2">Hourly</option>
</select>

<input name="hour" class="form-control" type="number" min="0" max="23" value="23" id="hourField">
<input name="minute" class="form-control" type="number" min="0" max="59" value="57" id="minuteField">  

When I run my webapp, and click the button that calls the function, it says that the intervalSelector function is not defined.当我运行我的 web 应用程序并单击调用 function 的按钮时,它表示未定义 intervalSelector function。 Any ideas on what the problem could be?关于问题可能是什么的任何想法?

I think you have a mistyping in your function definition, the );我认为您在 function 定义中输入错误, ); shouldn't be there, at line -2不应该在那里,在-2行

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

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