• 赚钱入口【需求资源】限时招募流量主、渠道主,站长合作;【合作模式】CPS长期分成,一次推广永久有收益。主动打款,不扣量;

SweetAlert如何等待用户操作确认?

JavaScript cps12345 1个月前 (10-29) 37次浏览 0个评论

我想等到SweetAlert弹窗的OK按钮点击后才执行操作

JS:

function myFunction() {
        swal({
            title: "Are you sure?",
            text: "You will not be able to recover this Job Title!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes, delete it!",
            closeOnConfirm: false,
            closeOnCancel: false
        }, function (isConfirm) {
            if (isConfirm){
                swal("Deleted!", "Job Title has been deleted.", "success");
            } else {
                swal("Cancelled", "Job Title was not deleted.", "error");
            }
        });
    };

HTML:

<a href="{{ path('setup_jobtitles_delete', {'id': jobTitle.id}) }}" onclick="myFunction()" class="btn btn-white" data-toggle="tooltip" data-toggle="tooltip" data-placement="top" title="Delete">
    <i class="fa fa-trash"></i>
</a>

解决

像如下操作

<a href="/delete/me" class="confirm" data-title="Want to delete this?" data-type="warning">Click me</a>

<a href="/edit/me" class="confirm" data-title="Info image" data-type="info">Click me</a>

需要jquery loaded

$(function() {
    $('a.confirm').click(function (e) {
            e.preventDefault();
            var tthis = $(this);
            swal({
                title: "Are you sure?",
                text: $(this).data('title'),
                type: $(this).data('type'),
                showCancelButton: true,
                confirmButtonText: "Yes",
                cancelButtonText: "No",
            }, function (isConfirm) {
                if (isConfirm) {
                    document.location.href = tthis.attr('href');
                }
            });
        });
});

等待用户操作可以使用 e.preventDefault();

喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址