jQuery阻止表單預(yù)設(shè)提交行為後,要繼續(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()阻止預(yù)設(shè)提交事件後,sweetalert2提示框可以正常彈出。
問題:
當點擊sweetalert2提示框的「確認刪除」後,要繼續(xù)提交這個表單,要怎麼寫?
給按鈕綁定點擊事件,在事件的回調(diào)函數(shù)中實現(xiàn)彈窗,並判斷用戶選擇的彈窗值,為真則獲取表單元素觸發(fā)提交事件$("form").submit()
不為真則做其他相應(yīng)處理
講道理,input的type寫成submit讓你需要額外寫阻止預(yù)設(shè)事件,type改成button,就不用阻止預(yù)設(shè)事件了。你的確認框應(yīng)該是有回呼函數(shù)的,或許就是then裡面,直接寫投稿應(yīng)該可以的