javascript - How can I create a if using the value of select?
问题描述
Hello I have the following code :
<select name="test1" class="form-control">
<option value=""></option>
{% for test in tests %}
<option value="{{ test.id }}">{{ test.name }}</option>
{% endfor %}
</select>
Then I want to do something like this ;
{% if test.id %}
# show something but I need to know if there is sometinh in test.id
# I think the problem comes from this line, in fact I want this condition is realised if test.id is not empty else the condition is not realised.
{% endif %}
How can I do this ?
解决方案
看看这种方法是否有帮助。
<select name="test1" class="form-control">
<option value=""></option>
{% for test in tests %}
<option value="{{ test.id }}">{{ test.name }}</option>
{% if test.id %}
#show what you want..
{% empty %} <<<<======
#if empty do this...
{% endif %}
{% endfor %}
</select>
只是一个例子:
import Http Response from django
from django.shortcuts import render
#create a function
def your_view(request):
# create a dictionary
context = {
#enter code here
"data" : [],
}
# return response
return render(request, "template.html", context)
现在在templates/template.html中,
{% for i in data %}
<div class="row">
{{ i }}
</div>
{% empty %}
<h4>There is nothing in this list</h4>
{% endfor %}
您可以更好地查看此链接: https ://www.geeksforgeeks.org/for-empty-loop-django-template-tags/
推荐阅读
- docusignapi - 文档签名收件人的 textTabs 更新上的 INVALID_TAB_OPERATION
- java - 向多个收件人发送邮件
- php - 无法从php返回数据做出反应
- android - 如何在安卓中使用 ASP.NET api
- angular7 - Cumulocity 服务器中的 CSS 样式问题
- mysql - 如何使用变量表修复“子查询返回超过 1 行”
- git - 克隆后 Git 签出到标签给出错误
- android - 应用程序在后台时不调用 FCM 的 onMesasageReceived?
- sql-server - SQL 存储过程的 select 语句中标量函数的备用选项
- reactjs - 如何在 reactjs 的其他应用程序中创建可重用组件?