我想要一个 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/