python - 如何在循环中选中多个复选框
问题描述
在一个数组中,我得到如下数组:
['1','2','3','4','5','6']
在另一个数组中,我得到的数组如下:
['1','6']
在模板中我这样做:
<div class="col-md-10">
{% if edit_data.features %}
{% for feature in edit_data.feature %}
{% for features in edit_data.features %}
{% if feature.id == features.feature_id %}
checked='checked'
{%endif%}
{%endfor%}
<input type="checkbox" name="features" value={{feature.id}} {{checked}}> {{feature.name}}
{%endfor%}
{%endif%}
</div>
在这之后我越来越喜欢:
我如何选择循环上的复选框我无法在 if 条件内创建变量。我是新手请让我现在如何使复选框被选中
[1]: https://i.stack.imgur.com/LzVV4.png
在您的解决方案之后,我得到了这样的结果:
解决方案
<div class="col-md-10">
{% if edit_data.features %}
{% for feature in edit_data.feature %}
{% with ns=namespace(found=false) %}
{% for features in edit_data.features %}
{% if feature.id == features.feature_id %}
{% with ns.found=true %}
{% endif %}
{%endfor%}
{% if ns.found == true %}
<input type="checkbox" name="features" value={{feature.id}} checked> {{feature.name}}</option>
{% else %}
<input type="checkbox" name="features" value={{feature.id}} > {{feature.name}}</option>
{% endif %}
{%endfor%}
{%endif%}
</div>
推荐阅读
- linux - 在控制台中为长行设置标准输入 unbufferd ... cat > textfile
- javascript - Node.js如何将pg数据库中的数据保存到变量中
- ansible - CentOS Stream 8 似乎不支持 Ansible tower/awx 3.8.3
- actionscript-3 - 如何根据动态文本的宽度创建动态下三分之一 i AS3
- playwright - Playwright 测试运行器:将测试限制为仅一个浏览器,其配置文件包含许多项目
- css - 打字机效果全幅
- jenkins - Jenkins 错误:在构建时找不到名为 toolbelt 的工具
- api - 是否有任何选项可以通过 PushBullet API 获取最后传入的短信?
- javascript - 如何从 JS 文件中的 groovy 控制器访问返回的对象
- selenium - XPATH 文本设置基于语言的动态值