javascript - What should I do if I want to continue the form submission behavior after jQuery blocks the form's default submission behavior?
漂亮男人
漂亮男人 2017-05-16 16:46:27
0
3
769

After jQuery blocks the default submission behavior of the form, what should I do if I want to continue the submission behavior?

The details are as follows:
A delete button, use sweetalert2 to prompt before submission. When the "Confirm" button in the prompt box is pressed, continue to submit the form.
html code:

<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 code:

    <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>

After preventDefault() prevents the default submission event, the sweetalert2 prompt box can pop up normally.

Question:
After clicking "Confirm Delete" in the sweetalert2 prompt box, what should I write if I want to continue submitting this form?

漂亮男人
漂亮男人

reply all(3)
巴扎黑

Bind the click event to the button, implement the pop-up window in the callback function of the event, and determine the pop-up window value selected by the user. If it is true, get the form element to trigger the submission event
$("form").submit()
If it is not true, do other corresponding processing

伊谢尔伦

To be fair, if the input type is written as submit, you need to write an additional block to block the default event. If the type is changed to button, there is no need to block the default event. Your confirmation box should have a callback function, maybe in then. You should be able to write submission directly

洪涛
if(!confirmed) {
    e.preventDefault();
    return;
}
$('form').submit();
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!