简体   繁体   English

JavaScript函数在Django上不起作用

[英]JavaScript Function Doesn't Work On Django

When the first page is loaded, I want the user to come across all the music, but if he selects a list from RadioButton, I only want the music in that list, but the javascript function doesn't work. 加载第一页时,我希望用户浏览所有音乐,但是如果他从RadioButton中选择一个列表,则我只希望该列表中的音乐,但javascript函数不起作用。

Let me add that I don't normally know JavaScript, but I need to use it. 让我补充一点,我通常不了解JavaScript,但是我需要使用它。

<div style = "margin-top : 100px;"class = "container">
        {% for table in tables %}
        <input type="radio" name="list1" onclick="mL('{{table}}')"> {{table}}
        {% endfor %}
        <div align="center">
            <audio class="my_audio" controls="controls" autoplay="autoplay" style="width:500px;"></audio>
            <ul>
            {% for table in tables  %}
                {% for music in musics %}
                    <li style="list-style-type:None">
                        <a id="{{table}}" href="javascript:void(0);" onclick="playSong('{{music}}')">{{music}}</a>
                    </li> 
                {% endfor %}
            {% endfor %}
                {% for music in musics %}
                    <li style="list-style-type:None">
                        <a id="default" href="javascript:void(0);" onclick="playSong('{{music}}')">{{music}}</a>
                    </li> 
                {% endfor %}

            </ul>
        </div>
    </div>

<script> function mL(x)
    {
        {% for table in tables  %}
            if (x=={{table}})
            document.getElementById("{{table}}").style.display="block";
            document.getElementById("default").style.display="none";
        {% endfor %}
            else
            document.getElementById("{{table}}").style.display="none";
            document.getElementById("default").style.display="block";
            return;
    }
    </script>

This isn't a Django issue, it's purely about the JavaScript (or should be). 这不是Django的问题,而纯粹是关于JavaScript(或应该是JavaScript)的问题。

Here are some problems with your code: 这是您的代码的一些问题:

  • In HTML, the id attribute should be unique in the document. 在HTML中, id属性在文档中应该是唯一的。 You seem to have many <a> tags with the same id. 您似乎有许多具有相同ID的<a>标记。 Use the class attribute, or even better use your own data attribute like data-table . 使用class属性,或者甚至更好地使用您自己的data属性,例如data-table
  • You can code generate JavaScript using Django template tags like you have, but I think it's a bad idea because it's very hard to reason about how the code will work. 可以像使用Django模板代码一样使用Django模板代码进行代码生成JavaScript,但是我认为这是个坏主意,因为很难推断出代码的工作方式。 Don't use {% for %} inside the <script> block. 不要在<script>块内使用{% for %}
  • JavaScript doesn't use spaces for code blocks like Python does. JavaScript不像Python那样对代码块使用空格。 You need to use braces { } instead. 您需要改用大括号{ }

Once those things are fixed, use the debugging console in your browser (F12 in Firefox and Chrome). 解决这些问题后,请使用浏览器中的调试控制台(Firefox和Chrome中为F12)。 It will let you see any syntax errors, and you can even run code in the console to see how things like document.getElementById work. 它可以让您看到任何语法错误,甚至可以在控制台中运行代码以查看诸如document.getElementById事情。

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

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