Twig 复选框已选中

问题描述 投票:0回答:4

请问我怎样才能用树枝复选框做如下事情

<input type="checkbox" name="checkbox">
{% if checkbox is checked %}
#do_it 
{% else %} 
#do_that 
{% endif %}

请帮帮我

symfony-forms
4个回答
0
投票

复选框返回字符串值。

{% if node.field_mycheckbox.value == '1' %}
  <p>print 1</p>
{% else %}
  <p>print 0</p>
{% endif %}

0
投票

控制器:

$dispo = $mezzo->getAuDisponibile ();
// table column AuDisponibile is boolean
...
...
return $this-render ('anagrafiche/automezzi.html.twig', ['dispo' => $dispo]);

模板:

{% if dispo  %}
<input input type='checkbox' name="idisponibile" value="SI" checked>
{% else %}
<input input type='checkbox' name="idisponibile" value="SI">
{% endif %}

0
投票

例如,您可以检查是否检查了表单字段,

{% if form.field.children.KEY.vars.checked %}
   Do something here
{% endif %}

在循环中使用它。

{% for child in form.field %}
<input type="checkbox" name="checkbox" {{ child.vars.checked == true ? "Checked" }}>
{% endfor %}

<input type="checkbox" name="checkbox" {{ form.field.children.KEY.vars.checked ? "Checked" }}>

KEY 是数组键。


0
投票

试试这个:

{% if checkbox.checked %}

而不是这个:

{% if checkbox is checked %}
© www.soinside.com 2019 - 2024. All rights reserved.