Jquery django:列表传输到模板和JS
在Views.py中,我创建了一个列表变量-Jquery django:列表传输到模板和JS,jquery,python,html,django,Jquery,Python,Html,Django,在Views.py中,我创建了一个列表变量- mortgages_counter.append(MonthlyPaymentAmount) 它被传输到.html模板,如下所示: <input id ='mortgages_counter' name='mortgages_counter' type='hidden' value='{{mortgages_counter}}'> 但根据console,它作为字符串传输到类似Jquery的['1234','125',由于某种原因,它的
mortgages_counter.append(MonthlyPaymentAmount)
它被传输到.html模板,如下所示:
<input id ='mortgages_counter' name='mortgages_counter' type='hidden' value='{{mortgages_counter}}'>
但根据console,它作为字符串传输到类似Jquery的['1234','125',由于某种原因,它的长度是1,
同时检查对索引[0]的访问是否给出-'和[1]-1等。
如何像使用列表而不是字符串一样使用此列表 您应该将该值作为字符串,您可以使用
join
filter来执行此操作。使用您知道不会在列表值中使用的分隔符。例如:
<input id ='mortgages_counter' name='mortgages_counter' type='hidden' value='{{mortgages_counter||join:"|" }}'>
尽管@JulienGregoire已经回答了这个问题,但我要说的是另一种方法。您可以通过
u计数器运行一个循环
,并将每个项目添加到输入
标记中。然后在Jquery中,使用遍历每个对象并进行检查
在模板中,执行类似的操作
{% for mortgages in mortgages_counter %}
<input id='mortgages_counter' name='mortgages_counter' type='hidden' value='{{ mortgages }}'>
{% endfor %}
$("#mortgages_counter").each(function () {
var mortgage = $(this).val();
/* do rest of the things and/or comparisons */
});
{% for mortgages in mortgages_counter %}
<input id='mortgages_counter' name='mortgages_counter' type='hidden' value='{{ mortgages }}'>
{% endfor %}
$("#mortgages_counter").each(function () {
var mortgage = $(this).val();
/* do rest of the things and/or comparisons */
});