如何从 Laravel 5.2 中的某些复选框中获取选中的复选框 ID
How to get selected checkbox ID from some checkboxes in Laravel 5.2
我正在 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
提前致谢..:)
您需要为此使用 JavaScript。在按钮的onclick事件中,只要得到checked checkbox,重定向到编辑好的url:
$('a.btn').click(function()
{
location.href = $(this).attr('href') + $('input[name="report"]:checked').attr('id');
});
请注意,我正在使用 jQuery 库。
我假设您正在尝试删除用户检查过的每一行。如果您的复选框位于表单元素内,则无需在 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
]
我还不能测试这个,所以如果这不起作用请告诉我。
我正在 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
提前致谢..:)
您需要为此使用 JavaScript。在按钮的onclick事件中,只要得到checked checkbox,重定向到编辑好的url:
$('a.btn').click(function()
{
location.href = $(this).attr('href') + $('input[name="report"]:checked').attr('id');
});
请注意,我正在使用 jQuery 库。
我假设您正在尝试删除用户检查过的每一行。如果您的复选框位于表单元素内,则无需在 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
]
我还不能测试这个,所以如果这不起作用请告诉我。