Laravel 5.1如何使用{{old('')}} helper在blade文件上进行无线电输入


Laravel 5.1 how to use {{ old('') }} helper on blade file for radio inputs

我目前正在学习laravel和创建我的第一个表单。一切都很棒,直到我想在刀片文件中使用{{old(")}} helper作为单选按钮。我不确定如何正确地去做,我似乎也找不到很多关于它的信息。

我的代码如下:

<div class="form-group">
    <label for="geckoHatchling">Gecko Hatchling?</label>
    <div class="radio">
        <label>
            <input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1">
            Yes
        </label>
    </div>
    <div class="radio">
        <label>
            <input type="radio" name="geckoHatchling" id="geckoHatchlingNo" value="0" checked>
            No
        </label>
    </div>
</div>

我认为下面的代码更简洁一些:

<input type="radio" name="geckoHatchling" value="1" @if(old('geckoHatchling')) checked @endif>
<input type="radio" name="geckoHatchling" value="0" @if(!old('geckoHatchling')) checked @endif>

@if正在检查旧值的真实性,在任何情况下都输出checked

您可以使用Form-helper,但它不会与Laravel开箱即用。您应该手动安装它。请阅读文档

与form帮助程序

1。叶片

{!! Form::radio('geckoHatchling', '1', (Input::old('geckoHatchling') == '1'), array('id'=>'geckoHatchlingYes', 'class'=>'radio')) !!}
{!! Form::radio('geckoHatchling', '0', (Input::old('geckoHatchling') == '0'), array('id'=>'geckoHatchlingNo', 'class'=>'radio')) !!}

2。PHP

echo Form::radio('geckoHatchling', '1', (Input::old('geckoHatchling') == '1'), array('id'=>'geckoHatchlingYes', 'class'=>'radio'));
echo Form::radio('geckoHatchling', '0', (Input::old('geckoHatchling') == '0'), array('id'=>'geckoHatchlingNo', 'class'=>'radio'));
没有form帮助程序

1。叶片

<input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1" @if(Input::old('geckoHatchling')) checked @endif>
<input type="radio" name="geckoHatchling" id="geckoHatchlingNo" value="0" @if(!Input::old('geckoHatchling')) checked @endif>

2。PHP

<input type="radio" name="geckoHatchling" value="1" class="radio" id="geckoHatchlingYes" <?php if(Input::old('geckoHatchling')== "1") { echo 'checked'; } ?> >
<input type="radio" name="geckoHatchling" value="0" class="radio" id="geckoHatchlingNo" <?php if(Input::old('geckoHatchling')== "0") { echo 'checked'; } ?> >

我已经尝试了公认的答案的解决方案,它不适合我,

为了使用条件语句,我必须使用这个{{}}花括号(这是刀片模板的回声)。

<input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1" {{(old('geckoHatchling') == '1') ? 'checked' : ''}}>

我偶然发现了一个我认为更准确、更全面的答案。提供的主要答案的问题是,最后一个单选按钮将始终被选中向前移动。但是,我在下面提供了一个代码示例来展示如何解决这个问题。(PS -这适用于Laravel 8,而且,我只假设它将适用于以前的版本)

<div> <input type="radio" name="role" value="teacher" @if(old('role') == 'teacher') checked @endif> </div>
        
<div> <input type="radio" name="role" value="student" @if(old('role') == 'student') checked @endif> </div>
        
<div> <input type="radio" name="role" value="assistant" @if(old('role') == 'assistant') checked @endif> </div>
只用:

@if(old('role') checked @endif
如果您有多个单选按钮,

将始终导致最后一个单选按钮被选中。

<input type"radio" name="active" value="1" {{old('active',user->active))?'checked',''}}/>
<input type"radio" name="active" value="0" {{old('active',user->active))?'','checked'}}/>

Laravel 9

<input type"radio" name="active" value="1" @checked(old('active',$user->active))/>
<input type"radio" name="active" value="0" @checked(!old('active',$user->active))/>