【问题标题】:Continue execution of ajax request after altering with ajax:beforeSend用 ajax:beforeSend 修改后继续执行 ajax 请求
【发布时间】:2015-02-23 17:33:07
【问题描述】:

在我的 rails 应用程序中使用 jquery_ujs 处理 ajax 表单。我通过调用 ajax:beforeSend 事件更改了要使用 remote: true 发送的表单。如果在我使用 sweetalert 时确认操作执行,我想让它继续执行。

$('.content>.fr span:last-of-type>a').on "ajax:beforeSend", (xhr, settings) ->
        swal
          title: "Are you sure?"
          text: "You will not be able to undo this!"
          type: "warning"
          showCancelButton: true
          confirmButtonColor: "#DD6B55"
          confirmButtonText: "Yes, delete it!"
          closeOnConfirm: false
        , ->
          # should send the request here
          swal "Deleted!", "Department has been deleted.", "success"
          return

要查看它的完整版本,请访问this gist

【问题讨论】:

    标签: jquery ruby-on-rails ajax coffeescript ujs


    【解决方案1】:

    你应该喜欢这个

    $("body").on("click", "#element", function(e) {           
                 swal({
                    title: "Are you sure?",
                    text: "Are you sure?",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Yes, delete it!",
                    closeOnConfirm: false },
                    function(){
                       // ajax goes here
                    }       
                );
            });
    

    SweetAlert 与 javascript confirm 不同,脚本执行在等待用户输入时不会停止,因此如果 beforeSend 中的函数没有返回 false,则 ajax 会继续处理请求。

    【讨论】:

    • 现在在我的第二个 Rails 项目中,我什至从未尝试过 ajax,因为我一直在使用 Rails 进行开发。我只使用 UJS `remote: true`
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    相关资源
    最近更新 更多