2
我试图在我的Laravel应用程序中创建一个删除提醒,但问题是当我点击删除按钮时,提示出现但没有确认提醒按钮,删除表单被提交并且项目获取从数据库中删除。Laravel的SweetAlert实现
@foreach($album_names as $album)
<tr>
<td>{{ $album->album_name }}</td>
<td>
{!! Form::open(['url' => "album/{$album->id}", 'method' => 'delete','id'=>'delete_form']) !!}
<button class="btn btn-danger" id="delete">Delete</button>
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<script type="text/javascript">
$('button#delete').on('click', function(){
swal({
title : "Are you sure?",
text : "You will not be able to recover this Album!",
type : "warning",
showCancelButton : true,
confirmButtonColor: "#DD6B55",
confirmButtonText : "Yes, delete it!",
cancelBUttonText : "No, Cancel delete!",
closeOnConfirm : false,
closeOnCancel : false
},
function(isConfirm){
if(isConfirm){
swal("Deleted!","your album has been deleted", "success");
$("#delete_form").submit();
}
else{
swal("cancelled","Your album is safe", "error");
}
}
);});
</script>
尝试'$( '#键删除')上( '点击',功能(E){e.preventDefault(); ......' – Rayon
能否请您详细说明IM新入js:3 @RayonDabre –
这是否工作?我不知道修复..但是一个猜测...你需要防止按钮的默认行为,否则它会提交表格... – Rayon