在Views.py中我创建了一个列表变量 -
mortgages_counter.append(MonthlyPaymentAmount)
将其转移到.html模板:
<input id ='mortgages_counter' name='mortgages_counter' type='hidden' value='{{mortgages_counter}}'>
在JQuery中(单独的文件我必须检查是否使用了此列表的每个元素)。值将转移到.js文件:
var mortgages_counter = $('#mortgages_counter').val();
但根据控制台,它作为字符串传输到Jquery - 就像[&#39; 1234&#39; 125&#39;]由于某种原因它的长度为1, 也检查索引[0]的访问权限 - &#39;和[1] - 1等 如何使用LIST而不是字符串来操作此列表?
答案 0 :(得分:1)
虽然@JulienGregoire已经回答,但我提到了另一种方法。您可以在mortgages_counter
中运行循环,并将每个项目添加到input
代码中。然后在Jquery中使用.each()
遍历每个对象并检查。
在你的模板中做类似的事情。
{% for mortgages in mortgages_counter %}
<input id='mortgages_counter' name='mortgages_counter' type='hidden' value='{{ mortgages }}'>
{% endfor %}
在你的Jquery中使用这样的.each()
。
$("#mortgages_counter").each(function () {
var mortgage = $(this).val();
/* do rest of the things and/or comparisons */
});
答案 1 :(得分:0)
您应该将值设置为字符串,您可以使用function (a, b) {
return b - a;
}
过滤器来执行此操作。使用您知道不会在列表的值中使用的分隔符。像这样举例如:
join
请参阅:https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#join
然后使用相同的分隔符在javascript中拆分。它将字符串转换为数组:
<input id ='mortgages_counter' name='mortgages_counter' type='hidden' value='{{mortgages_counter||join:"|" }}'>