How to evaluate this condition in HTML jinja template based on id

僤鯓⒐⒋嵵緔 提交于 2019-12-08 13:50:31

问题


Assume i have

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

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


回答1:


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.

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.

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>


来源:https://stackoverflow.com/questions/58853346/how-to-evaluate-this-condition-in-html-jinja-template-based-on-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!