加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2021-07-06 05:25:12 所属栏目:大数据 来源: https://www.jb51.cc
导读:我使用的是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="na

我使用的是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>

我的问题是:

我可以在输入类型=“文本”中显示旧数据,如下所示:

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

我可以在input type =“radio”中显示旧数据,如下所示:

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

但我不知道如何在input type =“checkBox”中显示旧数据:

<input class="form-check-input" type="checkBox" name="hobby[]" value="3" @if(...) checked @endif>

怎么做?

解决方法:

这将有效:

        <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>

另据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>

要么

<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>

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读