我有一个表数据,该表具有功能删除按钮。 此按钮已成功删除,但我需要添加警报单击确认才能删除:
<a href="delete.php?id=<?=$row['id']; ?>"
onclick="return confirm('Anda yakin mau menghapus item ini ?')">[Hapus]</a>
但是我在服务器端使用Datatales,我不知道该放在哪里onclick
我的功能删除
public function indexDataTables_pns()
{
$pns = Data_pns::with('users','master_golongan','master_jabatan')->get();
return Datatables::of($pns)->addIndexColumn()
->addColumn('Nama', function ($pns) {
return '<a href="/pns/'.$pns->id.'" target="_blank">'.$pns->users->nama.'</a>';
})
->editColumn('edit', function ($pns) {
return '<a href="/edit_pns/'.$pns->id.'" target="_blank" class="btn btn-xs btn-success"><i class="glyphicon glyphicon-edit"></i></a>';
})
->editColumn('hapus', function ($pns) {
// THIS START HERE FOR DELETE FUNCTION
$c = csrf_field();
$m = method_field('DELETE');
return "<form action='/delete/$pns->id' method='POST'>
$c
$m
<button style='margin-left:10px; width: 30px;' type='submit'
class='btn btn-xs btn-danger delete'>
<i class='glyphicon glyphicon-remove-circle'></i>
</button>
</form>";
})
->rawColumns(['Nama' => 'Nama','hapus' => 'hapus','action' => 'action','edit'=>'edit'])
->make(true);
}
我可以在其中放置此onclick和此消息吗?
答案 0 :(得分:0)
您可以在删除表单的按钮上使用它,例如
<button style='margin-left:10px; width: 30px;' type='submit'
class='btn btn-xs btn-danger delete' onclick='return confirm("Anda yakin mau
menghapus item ini ?")'>
<i class='glyphicon glyphicon-remove-circle'></i>
</button>
或在您的表单中添加一个类,并在提交表单之前使用确认
<form action='/delete/$pns->id' method='POST' class='delete-form'>
现在将此脚本添加到您的视图文件中
<script>
$('.delete-form').submit(function(event){
if(!confirm('Anda yakin mau menghapus item ini ?')){
event.preventDefault();
}
});
</script>