【问题标题】:How to get selected checkbox ID from some checkboxes in Laravel 5.2如何从 Laravel 5.2 中的某些复选框中获取选定的复选框 ID
【发布时间】:2016-12-19 06:22:27
【问题描述】:

我正在我的 laravel 视图中创建一些复选框,如下所示:

<table class="table table-condensed table-bordered table-hover">
   <tr>
     <th>Report</th>
     <th>Type</th>
     <th>Date</th>
  </tr>
 @foreach($tableContent as $data)
  <tr>
  <td><input type="checkbox" name="report" value="reportValue" id="{{$data->id}}" >{{$data->title}} </input></td>
  <td><b>{{$data->report_type}}</b></td>
  <td>{{$data->created_at}}</td>
  </tr>
 @endforeach
 </table>
<a class="btn btn-default" href="/deletereport/"{{--how do I get checked box id here ? --}}">Delete</a>

所以当用户点击删除按钮时,它会调用路由/deletereport/idcheckedbutton...

但我不知道如何获得选定的按钮 ID,请帮助...:v

提前谢谢.. :)

【问题讨论】:

    标签: php checkbox laravel-5 laravel-blade


    【解决方案1】:

    我假设您正在尝试删除用户检查的每一行。如果您的复选框位于表单元素中,则您无需在 url 中传递 id,选中的值将在您的 $request 对象中可用。

    然后你必须将你的复选框名称更改为

    <input type="checkbox" name="reports[{{$data->id}}]" value="reportValue" id="{{$data->id}}" >{{$data->title}}</input>
    

    然后你就可以在你的控制器中访问这个数组了。

    $reports = $request->input('reports');
    

    数组看起来像这样:

    [
        0 => 34 // the '0' is the $data->id and the '34' is the value assigned to the input
    ]
    

    我还不能对此进行测试,所以如果这不起作用,请告诉我。

    【讨论】:

    • okie...非常感谢 Kevin 先生...今晚我会试试...:)
    • Kevin 先生,我现在正在尝试,但我对必须与复选框放在一起的表单感到困惑。我的意思是如何将我的表单连接到我的删除按钮...???
    • 我正在这样做:
      {{$data->title}}
    • 我对调用我的控制器路由感到困惑.. 因为 if log::info($request);它给了我一个空日志...
    • 您可以通过javascript触发表单上的提交事件,也可以将链接变成提交按钮。如果您选择按钮,请确保该按钮位于您尝试提交的表单内。此外,在您的函数中,您可以像这样声明 $request 变量:functionName(Request $request)。确保您的表单在 for 循环之外,否则您将拥有多个表单。
    【解决方案2】:

    您需要为此使用 JavaScript。在按钮的onclick事件中,只需将选中的复选框并重定向到编辑的url:

    $('a.btn').click(function()
    {
        location.href = $(this).attr('href') + $('input[name="report"]:checked').attr('id');
    });
    

    请注意,我使用的是 jQuery 库。

    【讨论】:

    • 我现在更喜欢使用纯 php Martin 先生... :) 有没有使用 php 的方法...?? :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 2018-08-02
    • 1970-01-01
    • 2013-06-07
    • 2011-12-10
    • 1970-01-01
    • 2016-01-29
    相关资源
    最近更新 更多