微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

同时使用dangerMode和input - sweetalert

如何解决同时使用dangerMode和input - sweetalert

我使用Sweet Alert,所以在执行危险操作之前我会弹出对话框。示例。

swal({
  title: "Are you sure?",text: "Once deleted,you will not be able to recover this imaginary file!",icon: "warning",buttons: true,dangerMode: true,})
.then((willDelete) => {
  if (willDelete) {
    swal("Poof! Your imaginary file has been deleted!",{
      icon: "success",});
  } else {
    swal("Your imaginary file is safe!");
  }
});

用户可以根据需要输入任何备注,这是可选的,不是必需的。所以会变成这个样子

enter image description here

所以我修改了这样的代码

function RequestUpload(value) {

                swal({
                    title: "Are you sure?",text: "Are you sure want to request to upload?",content: {
                        element: "input",attributes: {
                            placeholder: "Any remarks?",type: "text",},})
                    .then((willDelete,input) => {
                        if (willDelete) {

                            swal(`You typed: ${input}`);
                            //Call ajax here

                            
                        }
                        else {
                            swal(`Is not delete`);
                        }
                       
                    });

            
            }     

但我无法从输入中获取值,它一直显示 undefined

enter image description here

知道如何解决这个问题吗?

解决方法

输入值作为第一个参数提供。当您单击取消、在弹出窗口外部单击或按 ESC 时,您将获得 null 值,该值将关闭警报(即:触发您的 else)。否则,如果您单击“确定”,它将保留您的输入值:

.then((input) => {
  if (input !== null) {
    swal(`You typed: ${input}`);
    //Call ajax here
  } else {
    swal(`Is not delete`);
  }
});

function RequestUpload(value) {
  swal({
      title: "Are you sure?",text: "Are you sure want to request to upload?",icon: "warning",buttons: true,dangerMode: true,content: {
        element: "input",attributes: {
          placeholder: "Any remarks?",type: "text",},})
    .then((input) => {
      if (input !== null) {
        swal(`You typed: ${input}`);
        //Call ajax here
      } else {
        swal(`Is not delete`);
      }
    });
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。