Laravel 5.1 如何在刀片文件上使用 {{ old('') }} 助手进行无线电输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33141056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:30:29  来源:igfitidea点击:

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

phplaravelbladelaravel-5.1

提问by Andy Holmes

I'm currently learning laravel and creating my first form. Everything is awesome until I want to use {{ old('') }} helper in my blade file for radio buttons. I'm not sure how to go about doing it properly, and I can't seem to find much info on here about it either.

我目前正在学习 laravel 并创建我的第一个表单。一切都很棒,直到我想在我的刀片文件中使用 {{ old('') }} 帮助器作为单选按钮。我不知道如何正确地做这件事,而且我似乎也找不到关于它的太多信息。

The code I have is as follows:

我的代码如下:

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

回答by Tim

I think the following is a little bit cleaner:

我认为以下内容更清晰:

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

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

@ifis checking the truthiness of the old value and outputting checkedin either case.

@if正在检查旧值的真实性并checked在任何一种情况下输出。

回答by Peter Kota

You can use Form-helper, but it doesn't come out of the box with Laravel. You should install it manually. Please read the docs.

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

WITH FORM-HELPER

带表格助手

1. Blade

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

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'));

WITHOUT FORM-HELPER

没有表格助手

1. Blade

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

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'; } ?> >

回答by Aravindh Gopi

I have tried the accepted answer's solution, it doesn't work for me,

我已经尝试了接受的答案的解决方案,它对我不起作用,

I had to use this {{}}curly braces (which is blade template's echo) in order to use the conditional statement.

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

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