如何解决有没有办法在Sweetalert,Reactjs中添加带有输入字段的取消按钮
我正在尝试在 ReactJs 中创建一个 Swal 警报,该警报必须包含一个带有继续和取消按钮的输入字段。这是我现在的代码:
swal({
icon: "warning",text: 'Are you sure you want to continue? This operation is not reversible',content: {
element: "input",attributes: {
placeholder: title + " reason",},}).then(comment => {
if (comment === null || comment === ''){
swal({icon: 'error',text: 'You need to type the cancellation reason'});
return false
}
})
此代码仅创建了一个“确定”按钮,我在 comment 变量中获得了用户的输入,但我需要添加一个“继续”按钮和一个“取消”按钮才能离开警报。我尝试在 swal 选项中添加 button 参数,这会创建按钮,但我无法从用户那里获得输入。
我还尝试在内容中添加原始 html 并使用 handle 方法获取输入,但到目前为止我还没有做到这一点。
也许这个解决方案很简单,但我无法得到它。非常感谢帮助
解决方法
我找到了解决这个问题的方法,以防万一有人不得不做类似的事情。我刚刚通过执行 document.createElement('input') 创建了输入,然后我根据用户点击了继续或取消获得了值:
const cancellation_reason = document.createElement('input');
swal({
icon: "warning",text: 'Are you sure you want to continue? This operation is not reversible',content: {
element: cancellation_input,attributes: {
placeholder: title + " reason",},buttons: {
continue: 'continue',cancel: 'cancel'
}
}).then(option => {
switch (option){
case 'continue':
if (cancellation_input.value === null || cancellation_input.value === ''){
swal({icon: 'error',text: 'You need to type the cancellation reason'});
return false
}
prop(runID,null,path,cancellation_input.value).then(showMesagge);
break;
case 'cancel':
break;
}
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。