Home  >  Q&A  >  body text

javascript - What should I do if I want to continue the form submission behavior after jQuery blocks the form's default submission behavior?

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?

漂亮男人漂亮男人2606 days ago663

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-05-16 16:48:27

    给按钮绑定点击事件,在事件的回调函数中实现弹窗,并判断用户选择的弹窗值,为真则获取表单元素触发提交事件
    $("form").submit()
    不为真则做其他相应处理

    Reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 16:48:27

    讲道理,input的type写成submit让你需要额外写阻止默认事件,type改成button,就不用阻止默认事件了。你的确认框应该是有回调函数的,或许就是then里面,直接写提交应该可以的

    Reply
    0
  • PHP中文网

    PHP中文网2017-05-16 16:48:27

    if(!confirmed) {
        e.preventDefault();
        return;
    }
    $('form').submit();

    Reply
    0
  • CancelReply