2

我想要你 - 我的网站的SweetAlert。现在我需要使用此代码配置 SweetAlert,以便在单击“确定”按钮时,它将通过 POST formaction="/link/link" post="test=1" 发送

swal({
    title: "Are you sure?",
    text: "You will not be able to recover this imaginary file!",
    type: " warning ",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!",
    cancelButtonText: " No, cancel plx!",
    closeOnConfirm: false,
    closeOnCancel: false
}, function(isConfirm) {
    if (isConfirm) {
        swal("Deleted!", "Your imaginary file has been deleted.", "success");
    } else {
        swal("Cancelled", "Your imaginary file is safe: ) ", " error ");
    }
});

我想问你,我该如何构建它。

4

4 回答 4

5

在 swal 回调中添加 POST 请求触发器,如下所示:

swal({
    title: "Are you sure?",
    text: "You will not be able to recover this imaginary file!",
    type: " warning ",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!",
    cancelButtonText: " No, cancel plx!",
    closeOnConfirm: false,
    closeOnCancel: false
}, function(isConfirm) {
    if (isConfirm) {
        swal("Deleted!", "Your imaginary file has been deleted.", "success");
        post('/link/link', {test: '1'});  // <<< This is what I added
    } else {
        swal("Cancelled", "Your imaginary file is safe: ) ", " error ");
    }
});

函数的定义在post()这里找到

于 2014-11-20T10:04:02.193 回答
2

我用 Promise 实现了 Preconfirm 方法......

this.$swal({
            title: '¿Sure?',
            showCancelButton: true,
            confirmButtonText: 'Yes',
            cancelButtonText: 'No',
            showLoaderOnConfirm: true,
            preConfirm: function(result) {
                return new Promise(function(resolve, reject) {
                    if (result) {
                        axios.post('url', {data:data})
                        .then(function(response){
                            toastr.success('Succes');
                            resolve();
                        })
                        .catch(function(error){
                            toastr.error('Error ');
                            console.log(error);
                            reject();
                        })
                    }
                });
            },
            allowOutsideClick: () => !this.$swal.isLoading(),
        })
于 2018-04-19T13:53:50.787 回答
1

对于纯 JS - 这将发布post带有值的参数post123

var formTest = document.createElement('form');
formTest.setAttribute("method", "post");
formTest.setAttribute("action", "");

var post = document.createElement("input");
post.setAttribute("type", "hidden");
post.setAttribute("name", "post");
post.setAttribute("value", "post123");
formTest.appendChild(post);

document.body.appendChild(formTest);
formTest.submit();

如果你愿意,你可以使用需要 jQuery 的简短而漂亮的 AJAX 调用

于 2014-11-20T09:28:23.153 回答
0

试试这个代码,只需根据您的需要进行编辑。

swal({
 title: "Are you sure?",
 text: "You will not be able to recover this imaginary file!",
 type: " warning ",
 showCancelButton: true,
 confirmButtonColor: "#DD6B55",
 confirmButtonText: "Yes, delete it!",
 cancelButtonText: " No, cancel plx!",
 closeOnConfirm: false,
 closeOnCancel: false },
  function() {
    $.ajax({
      type: "POST",
      data: {
        'data_id': id
      },
      url: 'http://localhost/project/method',
      success: function(data) {
        swal("Deleted!", "Your imaginary file has been deleted.", "success");
      },
      error: function(data) {
        swal("Cancelled", "Your imaginary file is safe: ) ", " error ");
      }
    });
  }
于 2016-03-30T08:10:28.000 回答