如何在基于id的HTML jinja模板中评估此条件 [英] How to evaluate this condition in HTML jinja template based on id

查看:72
本文介绍了如何在基于id的HTML jinja模板中评估此条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

      <tr>
        <th>Action</th>
        <td id="action_id"></td>
      </tr>

现在,我想编写一个条件,在该条件下,我将根据action_id的值显示某些内容,即

Now i want to write a condition where i display something based on the value of action_id, i.e

{% if action_id == "on" %}
     display
{%else%}

我正在尝试显示此内容,这与

I am trying to display this , this is related to How to populate a submit popup based on the value you filled to evaluate a condition | flask

推荐答案

Jinja2模板引擎只能访问那些通过view函数传递给它的变量,并且由于您要评估的变量是HTML元素您需要访问此变量并使用javascript进行检查.

Jinja2 Template Engine has access to only those variables which are passed to it via the view function, and since the variable which you are trying to evaluate is an HTML element you need to access this variable and do your checking using javascript.

模板引擎不知道此变量是什么,因为它既不会通过您的视图函数传递,也不会在模板内部声明,因此您的if条件始终会失败.

Template Engine does not know what this variable is since it is neither passed via your view function nor it is declared inside the template, hence your if condition always fails.

您可以使用标准javascript,方法是使用script标签将其放在模板的末尾.

You can achieve this using standard javascript by putting it at the end of your template using the script tag.

<script type="text/javascript">
let id_element = $(#action_id).val()
if (id_element === 'on'){
// do your stuff here
}
</script>

这篇关于如何在基于id的HTML jinja模板中评估此条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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