Laravel 个来自同一组且带有可点击标签的复选框

Laravel checkboxes from same group with clickable labels

在 Laravel 中,我想显示一个带有复选框及其标签的表单。我这样做:

        {{ Form::open(array('url'=>'fancyurl', 'method' => 'post')) }}
            <p>Categories:</p>
            <ul>
                @foreach($categories as $c)
                    <li>
                        {{ Form::checkbox('categories[]', $c->id) }}
                        {{ Form::label('categories', $c->name) }}
                    </li>
                @endforeach
            </ul>
            <p>
                {{ Form::submit('Submit') }}
            </p>
        {{ Form::close() }}

它完美地显示了复选框及其标签。但是当我点击一个标签时,标签的复选框仍然没有被选中。 如何在 Laravel 中设置 "for" 属性?

The for attribute of the label tag should be equal to the id attribute of the related element to bind them together.

http://www.w3schools.com/tags/tag_label.asp

您需要在复选框上指定 id 属性:

{{ 
    Form::checkbox(
      'categories[]', 
      $c->id, 
      null, 
      ['id' => 'category-' . $c->id]
    ) 
}}
{{ Form::label('category-' . $c->id, $c->name) }}

来源:https://github.com/illuminate/html/blob/master/FormBuilder.php#L567