如何在django模板中为标签创建动态标识 [英] How to create dynamic ids for tags in django templates

查看:132
本文介绍了如何在django模板中为标签创建动态标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我有一个动态表(因为我不知道它的大小/元素,直到运行时),我试图用javascript函数填充文本区域。要做到这一点,我计划将文本区域的id与我想要的值一起传递到javascript函数中。



问题是我在创建每个文本输入字段的动态id值。这是我目前正在尝试做到这一点:

  {%withinput_| add:applicant.id as idName% } 
< input id ={{idName}}type =textvalue =>
< input type =buttonhidden =TRUEonclick =>
{{idName}}
< script>
putTags({{idName}},{{tags}});
< / script>
{%endwith%}

其中函数putTags()将填充文本输入的内容。不幸的是,这不起作用,因为它将每个人的ID分配给input_而没有追加applicant.id的值(并且我已经检查过,applicant.id对于每次迭代都有一个正确的ID)。难道我做错了什么?有没有更简单的方法来创建这些唯一的ID?

解决方案

您可以尝试这样的事情

 < input id =input _ {{applicant.id}}type =textvalue => 
< input type =buttononclick =putTags('input _ {{applicant.id}}',{{tags}});>


Background: I have a dynamic table (as in I don't know its size/elements until runtime) where I am trying to populate a text area with a javascript function. To do this I plan on passing the text area's id along with the values I want to populate it with into the javascript function.

The problem is I am having trouble creating a dynamic id value for each text input field. This is how i am currently attempting to do this:

   {% with "input_"|add:applicant.id as idName %}
        <input id="{{ idName }}" type="text" value="">
        <input type="button" hidden="TRUE" onclick="">
        {{ idName }}
        <script>
            putTags({{ idName }}, {{ tags }});
        </script>
   {% endwith %}

where the function putTags() will populate the text input's contents. Unfortunately this doesn't work, as it assigns everyone's id to "input_" without appending applicant.id's value (and I have checked, applicant.id has a correct id for each iteration). Am i doing something wrong? Is there an easier way to create these unique IDs?

解决方案

You can try something like this

<input id="input_{{ applicant.id }}" type="text" value="">
<input type="button" onclick="putTags('input_{{ applicant.id }}', {{ tags }});">

这篇关于如何在django模板中为标签创建动态标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆