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