【发布时间】:2019-07-24 04:37:39
【问题描述】:
我有一个使用 for 循环打印 cmets 的 django 模板,我想在单击编辑链接时显示输入字段和一个按钮,我该怎么做。 并且当按下编辑按钮时,我想从该特定输入字段中获取值。我该怎么做?
{% for comment in comments %}
<div class="comment mdl-color-text--grey-700">
<header class="comment__header">
<img src="images/co1.jpg" class="comment__avatar">
<div class="comment__author">
<strong>{{comment.User_name}}</strong>
<span>{{comment.date}}</span>
</div>
</header>
<div class="comment__text">
{{comment.text}}
</div>
<!-- FAB button , class "mdl-button--fab"-->
<a href="javascript:delete_comment('{{comment.text}}','{{comment.blogslug}}')">
<button class="mdl-button mdl-js-button mdl-button--fab">
<i class="material-icons">delete</i>
</button>
</a>
<!--when this link is clicked bellow edit_comment div should appear -->
<a href="">
<button class="mdl-button mdl-js-button mdl-button--fab">
<i class="material-icons">edit</i>
</button>
</a>
<!-- and also when i click the edit button how can i get value from the input -->
<div id="edit_comment" style="visibility: hidden;">
<input type="text" name="edit_comment">
<button>edit</button>
</div>
</div>
{% endfor %}
所以问题是这种类型的许多其他 cmets 因为它们是使用循环打印的。
【问题讨论】:
-
使用
{{ forloop.counter }}在 for 循环中获取一个计数器,并将其添加到divs 的id属性中。单击按钮时,将其作为参数传递给您的 JavaScript 函数。