简体   繁体   English

Javascript无法在Django循环中刷新

[英]Javascript doesn't refresh in Django loop

I have a Django template (html), and a javascript in it that I want to loop for various pair values. 我有一个Django模板(html)和一个要在其中循环获取各种对值的javascript。 It doesn't loop. 它不会循环。

Here is the code in the template rsmgui.html: 这是模板rsmgui.html中的代码:

{% for field in elements %}

            <input type="hidden" id="theFieldLabelID" name="theFieldLabel" value="{{ field.label }}">
            <input type="hidden" id="theFieldID" name="theField" value="{{ field }}">

            <script src="{{ STATIC_URL }}js/loadStorage.js"></script>
{% endfor %}

The javascript loadStorage.js looks like this: javascript loadStorage.js看起来像这样:

var myFieldLabel=document.getElementById("theFieldLabelID").value.replace(/ /g,"")

var myField = document.getElementById("theFieldID")
alert("Label = " + myFieldLabel);
localStorage.setItem(myFieldLabel, JSON.stringify(myField));

But it doesn't loop, it gets the first pair and then repeats it for the number of pairs. 但是它不会循环,它获取第一对,然后重复对数。 Any ideas how to "flush" the javascript so it reloads each time? 任何想法如何“刷新” JavaScript,以便每次重新加载?

it gets the first pair and then repeats it for the number of pairs 它得到第一对,然后重复对数

That is because getElementById only returns first element . 这是因为getElementById仅返回第一个element Element ID should be unique across the page. 元素ID在整个页面上应该是唯一的。 Without changing your logic, there are two solutions below. 在不更改逻辑的情况下,下面有两种解决方案。

First one is to add loop counter suffix: 第一个是添加循环计数器后缀:

{% for field in elements %}

            <input type="hidden" id="theFieldLabel{{ forloop.counter }}" name="theFieldLabel" value="{{ field.label }}">
            <input type="hidden" id="theField{{ forloop.counter }}" name="theField" value="{{ field }}">

            <script>var id_suffix = "{{ forloop.counter }}"</script>
            <script src="{{ STATIC_URL }}js/loadStorage.js"></script>
{% endfor %}
var myFieldLabel=document.getElementById("theFieldLabel" + id_suffix).value.replace(/ /g,"")

var myField = document.getElementById("theField" + id_suffix)
alert("Label = " + myFieldLabel);
localStorage.setItem(myFieldLabel, JSON.stringify(myField));

Second one is to use getElementsByName: 第二个是使用getElementsByName:

{% for field in elements %}

            <input type="hidden" id="theFieldLabelID" name="theFieldLabel" value="{{ field.label }}">
            <input type="hidden" id="theFieldID" name="theField" value="{{ field }}">

            <script>var index = {{ forloop.counter0 }}</script>
            <script src="{{ STATIC_URL }}js/loadStorage.js"></script>
{% endfor %}
var myFieldLabel=document.getElementsByName("theFieldLabel")[index].value.replace(/ /g,"")

var myField = document.getElementsByName("theField")[index]
alert("Label = " + myFieldLabel);
localStorage.setItem(myFieldLabel, JSON.stringify(myField));

Considering @sytech comment, you can wrap your code into function with a parameter is the index/id_suffix. 考虑@sytech注释,您可以将代码包装为具有index / id_suffix参数的函数。 Then call it with necessary value. 然后用必要的值调用它。

<script src="{{ STATIC_URL }}js/loadStorage.js"></script>
{% for field in elements %}

            <input type="hidden" id="theFieldLabel{{ forloop.counter }}" name="theFieldLabel" value="{{ field.label }}">
            <input type="hidden" id="theField{{ forloop.counter }}" name="theField" value="{{ field }}">

            <script>loadStorage("{{ forloop.counter }}");</script>
{% endfor %}
function loadStorage(id_suffix)
{
    var myFieldLabel=document.getElementById("theFieldLabel" + id_suffix).value.replace(/ /g,"")

    var myField = document.getElementById("theField" + id_suffix)
    alert("Label = " + myFieldLabel);
    localStorage.setItem(myFieldLabel, JSON.stringify(myField));
}

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

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