【问题标题】:How to continue to submit a form after preventing it with jQuery?使用jQuery阻止表单后如何继续提交表单?
【发布时间】:2017-05-14 18:00:19
【问题描述】:

用jQuery阻止后如何继续提交表单?

例如:
有一个删除input元素:

<form action="/articles/{{ $article->id }}" method="POST">
    {{ method_field('DELETE') }}
    {{ csrf_field() }}
    <input class="btn btn-danger btn-sm need-alert" type="submit" value="delete">
</form>

我想在提交表单之前给用户一个sweetalert2 提醒。

Javascript code:

<script>
    $('.need-alert').on('click', function (event) {

        event.preventDefault();

        //sweetalert2 code
        swal({
            title: 'Are you sure?',
            text: "You won't be able to revert this!",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, delete it!'
        }).then(function () {

             //...

        }).catch(swal.noop);
    })
</script>

sweetalert2 警告框可以正确显示。

问题:
点击sweetalert2提示框的确认按钮后如何继续提交表单?

【问题讨论】:

  • 给你的表单一个 id 然后 $("#formId").submit ()
  • 点击警告框确认按钮后,触发 $("#form").submit();
  • @M. Gara 和 Akshay Chawla,它不起作用。
  • 试试 return true;而不是 $("#form").submit();

标签: jquery laravel-5 sweetalert2


【解决方案1】:

您可以使用jQuery's submit function 来做到这一点。

<form id="deleteForm" action="/articles/{{ $article->id }}" method="POST">
    {{ method_field('DELETE') }}
    {{ csrf_field() }}
    <input class="btn btn-danger btn-sm need-alert" type="submit" value="delete">
</form>

<script>
    $('.need-alert').on('click', function (event) {

        event.preventDefault();

        //sweetalert2 code
        swal({
            title: 'Are you sure?',
            text: "You won't be able to revert this!",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, delete it!'
        }).then(function () {
            $('#deleteForm').submit();
        }).catch(swal.noop);
    })
</script>

处理提交事件比单击删除按钮更好。如果用户使用键盘导航到按钮,则不会显示确认。您可以通过以下方式解决此问题:

$(document).ready(function () {
        var form = $("#deleteForm");
        // keep track of the swall popup answer.
        var isConfirmed = false;

        form.submit(function (event) {
            // If answered "ok" previously continue submitting the form. 
            if (isConfirmed) {
                return true;
            }

            // Confirm is not ok yet so prevent the form from submitting and show the message.
            event.preventDefault();

            //sweetalert2 code
            swal({
                title: 'Are you sure?',
                text: "You won't be able to revert this!",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it!'
            }).then(function (confirmed) {
                isConfirmed = confirmed;
                form.submit();
            }).catch(swal.noop);
        });
    });
<link href="https://cdn.jsdelivr.net/sweetalert2/4.0.5/sweetalert2.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/sweetalert2/4.0.10/sweetalert2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form id="deleteForm" action="/articles/{{ $article->id }}" method="POST">
    <!-- {{ method_field('DELETE') }}
    {{ csrf_field() }} -->
    <input class="btn btn-danger btn-sm need-alert" type="submit" value="delete">
</form>

【讨论】:

  • 我试了下还是不行。看来回调函数中的代码没有运行。
  • @zwl1619 我更改了代码并添加了一个sn-p。这行得通。但我不确定这是否是最好的解决方案。 (我评论了“csrf”字段以使 sn-p 工作。)
猜你喜欢
  • 2020-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 2013-09-18
  • 1970-01-01
相关资源
最近更新 更多