0
我在編碼PHP laravel初學者,請幫助laravel確認刪除
我用laravel 5.3,我想打一個確認使用刪除SweetAlert
我已經安裝槽"npm install"
和它已經在node_modules
文件夾
的問題是什麼我必須在使用這個模塊去完成。
謝謝
我在編碼PHP laravel初學者,請幫助laravel確認刪除
我用laravel 5.3,我想打一個確認使用刪除SweetAlert
我已經安裝槽"npm install"
和它已經在node_modules
文件夾
的問題是什麼我必須在使用這個模塊去完成。
謝謝
;
JS代碼
$('button#deleteButton').on('click', function(e){
var name = $(this).data('name');
e.preventDefault();
swal({
title: "Careful!",
text: "Are you sure you want to delete "+name+"?",
icon: "warning",
dangerMode: true,
buttons: {
cancel: "Exit",
confirm: "Confirm",
},
})
.then ((willDelete) => {
if (willDelete) {
$(this).closest("form").submit();
}
});
});
鑑於您在窗體中創建一個按鈕,並與被刪除的元素的名稱添加數據的名稱;
<button type="submit" id="deleteButton" data-name="{{ $model->name }}" class="btn btn-xs btn-danger">Delete</button>
先加下面addDeleteForms功能的JavaScript功能齊全
/**
* Allows you to add data-method="METHOD to links to automatically inject a form
* with the method on click
*
* Example: <a href="{{route('customers.destroy', $customer->id)}}"
* data-method="delete" name="delete_item">Delete</a>
*
* Injects a form with that's fired on click of the link with a DELETE request.
* Good because you don't have to dirty your HTML with delete forms everywhere.
*/
function addDeleteForms() {
$('[data-method]').append(function() {
if (! $(this).find('form').length > 0)
return "\n" +
"<form action='" + $(this).attr('href') + "' method='POST' name='delete_item' style='display:none'>\n" +
" <input type='hidden' name='_method' value='" + $(this).attr('data-method') + "'>\n" +
" <input type='hidden' name='_token' value='" + $('meta[name="_token"]').attr('content') + "'>\n" +
"</form>\n";
else
return "";
})
.removeAttr('href')
.attr('style', 'cursor:pointer;')
.attr('onclick', '$(this).find("form").submit();');
}
例如elete按鈕,就像爲:
<a href="{{route('customers.destroy', $customer->id)}}"
data-method="delete"
data-trans-button-cancel="Cancel"
data-trans-button-confirm="Delete"
data-trans-title="Are you sure?"
class="btn btn-xs btn-danger"><i class="fa fa-trash" data-toggle="tooltip" data-placement="top" title="Delete"></i></a>
代碼添加到您的刪除按鈕使用它? – ceejayoz