如何在 Laravel 中将单选按钮值作为路由参数传递?

我想将选定的单选按钮值作为 Laravel 路由的参数传递。


我的路线是:


Route::resource('/datas','DataController');

从这条路线,我的目标是调用 localhost:8000/datas/{data}


我的 data.blade.php 是:


<form id="dataform" action="{{ route('datas.show')}}" method="GET">

   <table class="table">

      <tr>

         <th>Select bullet</th>

         <th>SL NO</th>

         <th>Name</th>

         <th>Age</th>

      </tr>

      @foreach ($datas $key => $data)

         <tr>

            <td><input type="radio" id="data{{$data->id}}" name="data" value={{$data->id}}></option></td>

            <td>{{$key}}</td>

            <td>{{$data->name}}</td>

            <td>{{$data->age}}</td>

         </tr>

      @endforeach

   </table>

   <button type="submit" id="edit_submit" class="btn btn-default">Show</button>                    </form>

我的表演功能将是...



public function show($id)

   {

      //Code to showing data and redirect to show page

   }

我想获取此单选按钮的值(如下所示)。


<td><input type="radio" id="data{{$data->id}}" name="data" value={{$data->id}}>

并将其作为参数包含在下面的表单操作中


<form id="dataform" action="{{ route('datas.show')}}" method="GET">


回首忆惘然
浏览 100回答 2
2回答

天涯尽头无女友

我对此代码做了一些更改。将按钮更改为锚标记。所以我的blade.php是:-&nbsp; &nbsp;<table class="table">&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th>Select bullet</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th>SL NO</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th>Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th>Age</th>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; @foreach ($datas $key => $data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type="radio" id="data{{$data->id}}" name="data" value={{$data->id}}></option></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{$key}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{$data->name}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{$data->age}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp;</table>&nbsp; &nbsp;<a&nbsp; href="javascript:showfunction()" id="showlink" class="btn btn-default">Show</a>我还添加了一个 javascript 函数。<script>&nbsp; &nbsp; function showfunction(){&nbsp; &nbsp; &nbsp; &nbsp;var id = document.querySelector('input[name = "data"]:checked').value;&nbsp; &nbsp; &nbsp; &nbsp;var url = '{{route("admin.questions.show",":id")}}';&nbsp; &nbsp; &nbsp; &nbsp;url=url.replace(':id',id);&nbsp; &nbsp; &nbsp; &nbsp;document.location.href=url;&nbsp; &nbsp; }</script>

阿晨1998

仅使用 HTML/Blade 无法实现这一点。您需要 javascript(或 javascript 框架)才能使实时更改生效,因为表单操作路径中的参数取决于所选单选的值。action="{{&nbsp;route('datas.show',&nbsp;[&nbsp;'id'&nbsp;=>&nbsp;[insert&nbsp;id&nbsp;here]&nbsp;])}}"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5