问题
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