【问题标题】:Symfony create checkbox and radio button manually without form generatorSymfony 手动创建复选框和单选按钮,无需表单生成器
【发布时间】:2018-05-28 11:41:44
【问题描述】:

我需要手动创建复选框和单选按钮,无需生成器。这是我的代码:

          {% for d in items %}
                        <div class="inline field">
                            <div class="ui checkbox">
                                <input 
                                    type="checkbox" 
                                    {% if d.isComplete %}checked{% endif %} 
                                    tabindex="0" 
                                    class="hidden"
                                    name="cbx_{{d.id}}"
                                    >
                                <label>{{d.nama}}
                                    <a href="#">
                                        <i class="trash icon"></i>
                                    </a>
                                </label>
                            </div>
                        </div>
                        {% endfor %}

这是我的复选框示例代码,

请帮助如何正确创建它并创建可以得到所有检查的提交操作。我尝试在服务器端使用此代码:

if ($request->isMethod('POST')) 
{
            $cbx_1 = $request->get("cbx_1");
}

它太粗鲁了,请帮助我如何正确地制作它。

谢谢

【问题讨论】:

    标签: php symfony checkbox


    【解决方案1】:

    您应该将 Id 设置为值:

         {% for d in items %}
                        <div class="inline field">
                            <div class="ui checkbox">
                                <input 
                                    type="checkbox" 
                                    {% if d.isComplete %}checked{% endif %} 
                                    tabindex="0" 
                                    class="hidden"
                                    name="cbx[]"
                                    value="{{d.id}}"
                                    >
                                <label>{{d.nama}}
                                    <a href="#">
                                        <i class="trash icon"></i>
                                    </a>
                                </label>
                            </div>
                        </div>
         {% endfor %}
    

    之后,就可以很方便的获取选中的值了:

    if ($request->isMethod('POST')) 
    {
      $cbx= $request->get("cbx"); //Array of selected values
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      • 1970-01-01
      • 2011-10-18
      • 2013-02-03
      • 2012-12-14
      相关资源
      最近更新 更多