jQuery阻止表單默認提交行為后,要繼續(xù)進行提交行為,怎么做?
具體是這樣的:
一個刪除按鈕,在提交之前用sweetalert2提示一下,當按下提示框中的“確認”按鈕后,繼續(xù)提交表單。
html代碼:
<form action="/articles/{{ $article->id }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<input class="btn btn-danger btn-sm need-alert" type="submit" value="刪除">
</form>
js代碼:
<script>
$('.need-alert').on('click', function (event) {
//阻止默認提交事件
event.preventDefault();
//sweetalert2的提示框代碼:
swal({
title: '確定要刪除嗎?',
text: '刪除后不能恢復!',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: '確認刪除'
}).then(function () {
}).catch(swal.noop);
})
</script>
preventDefault()阻止默認提交事件后,sweetalert2提示框可以正常彈出。
問題:
當點擊sweetalert2提示框的“確認刪除”后,要繼續(xù)提交這個表單,要怎么寫?
給按鈕綁定點擊事件,在事件的回調函數(shù)中實現(xiàn)彈窗,并判斷用戶選擇的彈窗值,為真則獲取表單元素觸發(fā)提交事件$("form").submit()
不為真則做其他相應處理
講道理,input的type寫成submit讓你需要額外寫阻止默認事件,type改成button,就不用阻止默認事件了。你的確認框應該是有回調函數(shù)的,或許就是then里面,直接寫提交應該可以的