我在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>
当用户点击删除按钮时,它会调用route/deletereport/idcheckedbutton。
但是我不知道如何获得选定的按钮Id,请帮助…:v
提前感谢…:)
我假设您正在尝试删除用户选中的每一行。如果你的复选框在一个表单元素中,你不需要在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
]
我还没有能够测试这个,所以让我知道,如果这不起作用
需要使用JavaScript。在按钮的onclick事件中,只需获得选中的复选框并重定向到编辑过的url:
$('a.btn').click(function()
{
location.href = $(this).attr('href') + $('input[name="report"]:checked').attr('id');
});
注意我使用的是jQuery库