【问题标题】:How to get selected radio button value in Laravel如何在 Laravel 中获取选定的单选按钮值
【发布时间】:2020-01-12 00:56:05
【问题描述】:

大家好,如何获取选定的单选按钮值。我想将值存储在选中单选按钮的表上。

我的控制器代码传递给 View $result

        $result =     DB::table('table')
                                ->where('name', $name)
                                ->where('code', $code)
                                ->get();

        return  View::make('home.search_result')            
                       ->with('result', $result);

在我的视图代码中,我有单选按钮。

{{ Form::open(array('action' => 'BookingController@postValue',  'class'=>'well', 'method' => 'GET')) }}         
<table id="search" class="table table-striped table-hover">
    <thead>
        <tr>
            <th></th>
            <th>Name</th>
            <th>Code</th>                                                                                                              
        </tr>
    </thead>
    <tbody> 
        @foreach($result as $result)                                    
            <tr class="success">    
                <td>{{ Form::radio('result') }}
                <td>{{ $result->name}}</td>                 
                <td>{{ $result->code}}</td>                 
            </tr>                       
        @endforeach                           
    </tfoot>
</table>           

{{ Form::submit('Add', array('class' => 'btn btn-info')) }}         
{{ Form::close() }}         

因此,当我单击“添加”按钮时,我尝试将已检查的单选按钮值传递给我的 postValue 函数。

【问题讨论】:

    标签: laravel-4 radio-button return-value


    【解决方案1】:

    您需要给单选按钮一个值。你给它的只是一个名字。

    <td>{{ Form::radio('result', $result->code) }}
    

    然后在处理表单的控制器中,您可以获取值。

    $result = Input::get('result');
    

    【讨论】:

    • 不错。您的代码正在运行。但我有很多价值。我可以把它们放在数组里吗?
    • 如果您希望能够选择多个,您应该使用一个复选框,名称为result[]。然后Input::get('result'); 将包含一个值数组。
    • 不需要复选框。我只能选择一个值。这一个值内部有多个值。例如姓名、代码、年龄、日期时间等。您可以在链接link 下查看示例图片
    【解决方案2】:

    您需要为单选按钮命名。

    普通 HTML:&lt;input name="sex" type="radio" value="male"&gt;

    Laravel:{{ Form::radio('sex', 'male') }}

    Form::radio('name','value');
    

    Laravel 获取名称和值的参数。

    【讨论】:

      猜你喜欢
      • 2013-03-28
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 2011-07-28
      相关资源
      最近更新 更多