【问题标题】:SweetAlert2: Unexpected type of html! Expected "string", got objectSweetAlert2:意外的 html 类型!预期的“字符串”,得到对象
【发布时间】:2018-08-29 09:37:23
【问题描述】:

我试图让SweetAlert2 对话框显示两个按钮,代码如下:sn-p:

swal('Some text', {
  buttons: {
    buttonA: {
      text: 'Some text',
      value: 'buttonA',
      className: 'some-class'
    },
    buttonB: {
      text: 'Some other text',
      value: 'buttonB',
      className: 'some-class'
    }
  },
  buttonsStyling: false
}).then((result) => {
  switch (result) {
    case 'buttonA':
      // Do something
      break;
    default:
      // Do something else
  }
})

但我收到以下错误消息:

SweetAlert2:意外的 html 类型!预期的“字符串”,得到对象

另外,对话框只显示一个“确定”按钮

我做错了什么?

【问题讨论】:

  • 检查这个jsfiddle.net/5jLv12ed/2,它工作得很好。也许您的代码中有其他一些 javascript 重叠。
  • 这个jsfiddle.net/c1guw4f8/5 显示了问题,但我无法弄清楚与您提供的那个有什么区别
  • 我用的是最新版本,就是有区别。
  • 显然此功能已在 SweetAlert2 中被删除 :(

标签: javascript sweetalert2


【解决方案1】:

您已安装 SweetAlert2,但使用的是 SweetAlert 的 API。这两个是现在具有不同 API 的不同插件。

使用 SweetAlert2 的 API 获得所需的结果:

swal({
  title: 'Hello world!',
  confirmButtonText: 'Some text',
  cancelButtonText: 'Some other text',
  confirmButtonClass: 'some-class',
  cancelButtonClass: 'some-other-class',
  showCancelButton: true
}).then(function(result) {
  if (result.value) {
    console.log('button A pressed')
  } else {
    console.log('button B pressed')
  }
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@7"></script> 

【讨论】:

  • 感谢您提供此解决方案,很遗憾我们无法在 SweetAlert2 对话框中获得两个以上的按钮。
【解决方案2】:

使用最新的sweetalert

只需将您的 sweetalert 脚本文件替换为:

<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

here 是文档

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 2017-01-19
    相关资源
    最近更新 更多