【问题标题】:how to add sweetalert pop-up messages in html with jascript如何使用 javascript 在 html 中添加甜蜜的警报弹出消息
【发布时间】:2018-03-23 12:06:21
【问题描述】:

我正在尝试在单击编辑按钮时设置 sweetalert 弹出消息。

这里是示例代码:

<td><a href="/control/edituser/{{Values.id}}" onclick='a' class="btn btn-info">Edit</a></td>

脚本:

<script>
$('a').click(function(e){
e.preventDefault();
var link = $(this).attr('href');

swal({
    title: "Are you sure?",
    text: "By clicking 'OK' you will be redirected to the link.",
    type: "warning",
    showCancelButton: true
},
function(){
    window.location.href = link;
});
});
</script>

【问题讨论】:

  • JASCRIPT!!!!!!

标签: jquery html css sweetalert sweet.js


【解决方案1】:

关于您的代码伙伴有一些问题。首先,当您没有定义函数而是依靠 jQuery 来检测点击事件时,为什么要使用 onclick='a'?使用 jQuery 或使用 onClick 触发函数调用。

第二件事是您需要检查确认以触发您想要的操作。

<td><a href="/control/edituser/{{Values.id}}" class="btn btn-info">Edit</a></td>

<script>
  $('a').click(function(e) {
    e.preventDefault();
    var link = $(this).attr('href');

    swal({
        title: 'Are you sure?',
        text: "By clicking 'OK' you will be redirected to the link.",
        type: 'warning',
        showCancelButton: true
      },function(isConfirm){
        if (isConfirm){
          window.location.href = link;
        } else {
          // Handle rejection
        }
      });
    });
  </script>

进一步阅读:

How to use confirm using sweet alert?

Using an HTML button to call a JavaScript function

【讨论】:

  • 想解释一下发生了什么?
  • 我将代码复制并粘贴到我的模板中。但是,它显示编辑页面而不发出警报。
  • 可以给sweetalert和javscript的cdn
  • @metext 一个快速的谷歌搜索会为您提供您正在寻找的结果。
  • 我复制和粘贴一样。
【解决方案2】:

您在代码中使用了不推荐使用的选项(类型、showCancelButton 等)。

如果你只有一个,我建议你为编辑按钮/id 上课。

通过 jQuery 在其上绑定点击事件并使用 then() 方法进行 swal。

示例:

<a href="/" class="editButton">edit</a>

还有 javascript:

var $editButton = $('.editButton');

$editButton.on('click', function(e) {

  e.preventDefault();

  swal({
      title: 'Are you sure?',
      text: 'By clicking \'OK\' you will be redirected to the link.',
      icon: 'warning',
      buttons: {
          cancel: true,
          confirm: true
      }
  }).then(function(value) {
      if (value) {
          //  Either true or null
          window.location.href = "https://www.stackoverflow.com";
      }
  });

});

内联示例

如果您想内联,则将 onclick 属性添加到您的元素并通过它发送 url:

<a href="/" class="editButton" onclick="foo(event, 'https://www.stackoverflow.com');">edit</a>

还有 javascript:

function foo(event, url) {

    event = event || window.event;
    event.preventDefault();

    swal({
        title: 'Are you sure?',
        text: 'By clicking \'OK\' you will be redirected to the link.',
        icon: 'warning',
        buttons: {
            cancel: true,
            confirm: true
        }
    }).then(function(value) {
        if (value) {
            //  Either true or null
            window.location.href = url;
        }
    });
}

【讨论】:

  • 不...但是,没有发出警报,它显示编辑页面。
  • i' 在 window.location.href 中使用此 url /control/edituser/{{Values.id}}。
  • 我做了内联示例。通过 onclick 属性传递您的 url,然后在窗口重定向中使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2015-09-17
  • 1970-01-01
相关资源
最近更新 更多