如何在Laravel中显示复选框的旧数据?

zwl*_*619 13 php laravel

我正在使用Laravel 5.3,这是一个下面的表单演示:

<div class="container">
    <form>
        <div class="form-group row">
            <label for="name" class="col-sm-2 col-form-label">Name</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="name" name="name" value="{{ old('name', $student->name)}}">
            </div>
        </div>

        <fieldset class="form-group row">
            <legend class="col-form-legend col-sm-2">Gender</legend>
            <div class="col-sm-10">
                <div class="form-check">
                    <label class="form-check-label">
                        <input class="form-check-input" type="radio" name="gender" value="1" @if(old('gender',$student->gender)=="1") checked @endif>
                        Male
                    </label>
                </div>
                <div class="form-check">
                    <label class="form-check-label">
                        <input class="form-check-input" type="radio" name="gender" value="2" @if(old('gender',$student->gender)=="2") checked @endif>
                        Female
                    </label>
                </div>
            </div>
        </fieldset>
        <div class="form-group row">
            <label class="col-sm-2">Hobbies</label>
            <div class="col-sm-10">
                <div class="form-check">
                    <label class="form-check-inline">
                        <input class="form-check-input" type="checkbox" name="hobby[]" value="1" @if(...) checked @endif> football
                    </label>
                    <label class="form-check-inline">
                        <input class="form-check-input" type="checkbox" name="hobby[]" value="2" @if(...) checked @endif> basketball
                    </label>
                    <label class="form-check-inline">
                        <input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(...) checked @endif> swimming
                    </label>
                </div>

            </div>
        </div>
        <div class="form-group row">
            <div class="offset-sm-2 col-sm-10">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>
    </form>
</div>
Run Code Online (Sandbox Code Playgroud)

我的问题是:

我可以input type="text"像这样显示旧数据:

<input type="text" class="form-control" id="name" name="name" value="{{ old('name', $student->name)}}">
Run Code Online (Sandbox Code Playgroud)

我可以input type="radio"像这样显示旧数据:

<input class="form-check-input" type="radio" name="gender" value="1" @if(old('gender',$student->gender)=="1") checked @endif>
Run Code Online (Sandbox Code Playgroud)

但我不知道如何显示旧数据input type="checkbox":

<input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(...) checked @endif>
Run Code Online (Sandbox Code Playgroud)

怎么做?

Meh*_*kar 52

这将有效:

        <div class="form-check">
            <label class="form-check-inline">
                <input class="form-check-input" type="checkbox" name="hobby[]" value="1" @if(is_array(old('hobby')) && in_array(1, old('hobby'))) checked @endif> football
            </label>
            <label class="form-check-inline">
                <input class="form-check-input" type="checkbox" name="hobby[]" value="2" @if(is_array(old('hobby')) && in_array(2, old('hobby'))) checked @endif> basketball
            </label>
            <label class="form-check-inline">
                <input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(is_array(old('hobby')) && in_array(3, old('hobby'))) checked @endif> swimming
            </label>
        </div>
Run Code Online (Sandbox Code Playgroud)

另据brokekidweb建议:

<label class="form-check-inline">
    <input class="form-check-input" type="checkbox" name="hobby[]" value="1" {{ (is_array(old('hobby')) and in_array(1, old('hobby'))) ? ' checked' : '' }}> football
</label>
Run Code Online (Sandbox Code Playgroud)

要么

<label class="form-check-inline">
    <input class="form-check-input" type="checkbox" name="hobby[]" value="1" {{ (is_array(old('hobby')) && in_array(1, old('hobby'))) ? ' checked' : '' }}> football
</label>
Run Code Online (Sandbox Code Playgroud)

  • 需要检查is_array()couse如果hobby []将为空(未选中所有复选框)php将获得错误"<< in_array()期望参数2为数组,null给定>> (2认同)
  • 你可以用三元运算符`{{(is_array(old('hobby'))和in_array(1,old('hobby'))来清理它?'checked':''}}`Blade也允许你使用**和**代替**&&**.你的答案是按照书面说的. (2认同)

Nes*_*ert 5

您可以使用

<input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(in_array(3, old('hobby'))) checked @endif>
Run Code Online (Sandbox Code Playgroud)

或者您可以切换为使用Laravel Collective https://laravelcollective.com/docs/5.3/html#checkboxes-and-radio-buttons

并改为这样的代码:

{!! Form::checkbox('name', 'value', true) !!}
Run Code Online (Sandbox Code Playgroud)


Muh*_*mar 5

第一种方式

首先创建变量

$hob = old('hobby');

然后检查是否是数组

echo is_array($hob) ? (in_array(1, $hob)?'checked':NULL) : NULL;

然后应用它。

<input class="form-check-input" type="checkbox" name="hobby[]" value="1" <?php echo is_array($hob) ? (in_array(1, $hob)?'checked':NULL) : NULL; ?>> football

第二种方式

或者你可以这样做,但在这种情况下你不会创建变量

@if(is_array(old('hobby')) && in_array(1,old('hobby'))) checked @endif

然后应用它。

<input class="form-check-input" type="checkbox" name="hobby[]" value="1" @if(is_array(old('hobby')) && in_array(1,old('hobby'))) checked @endif > football