javascript - SweetAlert2:意外的 html 类型!期望 "string",得到对象

标签 javascript sweetalert2

我想要一个 SweetAlert2显示两个按钮的对话框,带有以下代码片段:

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: Unexpected type of html! Expected "string", got object

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

我做错了什么?

最佳答案

您已经安装了 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> 

关于javascript - SweetAlert2:意外的 html 类型!期望 "string",得到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52074221/

相关文章:

javascript - 用户登录后添加一些内容

javascript - 仅在甜蜜警报关闭后自动对焦

Angularjs 变量值第一次使用 sweetAlert 时未更新,成功

javascript - onClick 方法适用于第二次单击。但它应该在第一次点击时起作用

javascript - 寻找原型(prototype) slider /轮播

javascript - 突出显示 sweetAlert 中的默认文本输入

javascript - 如何返回SweetAlert2输入的值?

jquery - 如何在模态中使用sweetalert2?

javascript - 如何找到3个或更多连续字符?

javascript - 如何为表格单元格 insidehtml 创建 OnClick 事件