【问题标题】:Remove "OK" button from sweet alert dialog从甜蜜警报对话框中删除“确定”按钮
【发布时间】:2017-02-23 05:49:02
【问题描述】:

我正在使用 javascript sweetalert2 库。

我想从警告框中删除确定按钮,但我没有找到不显示此按钮的任何属性。

我正在使用计时器属性timer:1000 在一秒钟内关闭警报。 所以,我认为在这件事上没有使用确定按钮。

【问题讨论】:

  • 在您的配置中设置showConfirmButton:falseLink to the Documentation
  • swal({ content: 'Hello Test', button: false, })

标签: javascript jquery sweetalert sweetalert2


【解决方案1】:

您可以使用这些属性:

showCancelButton: false, // There won't be any cancel button
showConfirmButton: false // There won't be any confirm button

喜欢这个

swal({
  title: 'Auto close alert!',
  text: 'I will close in 2 seconds.',
  timer: 2000,
  showCancelButton: false,
  showConfirmButton: false
}).then(
  function () {},
  // handling the promise rejection
  function (dismiss) {
    if (dismiss === 'timer') {
      //console.log('I was closed by the timer')
    }
  }
)

【讨论】:

  • 使用我的应用程序:angular2 + sweetalert2,节省了我的时间! :)
【解决方案2】:

更新 2018 年 4 月 6 日

不再需要showCancelButton 和showConfirmButton。相反,您可以设置按钮:true 以显示两个按钮,或按钮:false 以隐藏所有按钮。默认情况下,只显示确认按钮。

所以现在而不是这样做

showCancelButton: false;

showConfirmButton: false;

做事

buttons: false;

Guides

【讨论】:

  • 这是最好的答案!
【解决方案3】:

您需要在配置中设置showConfirmButton:false

swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showConfirmButton:false,
  confirmButtonText: 'Yes, delete it!'
})

这是fiddle

【讨论】:

    【解决方案4】:

    这对我有用:$(".confirm").attr('disabled', 'disabled');

    我的功能:

    function DeleteConfirm(c){
      swal({   
                title: "Want to delete this item?",   
                text: "You will not be able to undo this action!",   
                type: "warning",   
                showCancelButton: true,   
                confirmButtonColor: "#DD6B55",   
                confirmButtonText: "Yes, delete it!",   
                closeOnConfirm: false 
            }, function(){ 
              $(".confirm").attr('disabled', 'disabled'); 
    
            });
    }
    

    【讨论】:

      【解决方案5】:
      swal({
      
          title: "Success",
          text: "Permissions assigned Successfully",
          icon: "success",
          closeOnClickOutside: false,
      })
      

      使用closeOnClickOutside: false, 它对我有用。

      【讨论】:

        【解决方案6】:

        这个接受的答案已被弃用。这是在 SweetAlert2 中隐藏或删除按钮的方法。

        {
          buttons: false,
        }
        

        【讨论】:

          【解决方案7】:

          甜蜜警报 2 | 2022 年更新

          Swal.fire({
              ...
              showConfirmButton: false, //hide OK button
              allowOutsideClick: false, //optional, disable outside click for close the modal
              ...
          });
          

          作为新文档here

          【讨论】:

            【解决方案8】:

            另一种方法可以做到这一点。

            Swal.fire({
              type: 'error',
              title: 'Cancelled',
              text: 'Your offer is safe ?',
              showConfirmButton: false,
              timer: 2000
            })
            <script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>

            升级到 SweetAlert2 的 v9.x

            重大变化 - 将 type 重命名为 icon

            Swal.fire({
              icon: 'error',
              title: 'Cancelled',
              text: 'Your offer is safe ?',
              showConfirmButton: false,
              timer: 2000
            })
            <script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>

            【讨论】:

              【解决方案9】:

              尝试将 showConfirmButton 属性设置为 false。

              Look at their docs

              【讨论】:

                【解决方案10】:

                在添加任何按钮之前,清除所有按钮,然后重新添加它们(假设警报名称为“A”)-

                A.getButtonTypes().clear();
                ButtonType OpenStorage=new ButtonType("Open Storage");
                A.getButtonTypes().addAll(OpenStorage,ButtonType.CANCEL,ButtonType.NEXT);
                

                希望对你有帮助!!!

                【讨论】:

                  【解决方案11】:

                  下面的代码对我有用

                  我只设置了buttons: false;

                  更新

                  swal({
                      title: 'Auto close alert!',
                      text: 'I will close in 2 seconds.',
                      timer: 2000,
                      showCancelButton: false,
                      showConfirmButton: false
                  });
                  

                  【讨论】:

                    【解决方案12】:

                    标题部分:

                    <link rel="stylesheet" href="https://sweetalert2.github.io/styles/bootstrap4-buttons.css">
                    
                    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
                    

                    正文部分添加一个按钮:

                    <a href="" id="delete" class="btn btn-danger">Delete</a>
                    

                    jQuery 部分:

                    $(document).on("click", "#delete", function (e) {
                            e.preventDefault();
                            var link = $(this).attr("href"); const swalWithBootstrapButtons = swal.mixin({   customClass: {
                        confirmButton: 'btn btn-success',
                        cancelButton: 'btn btn-danger'   },   buttonsStyling: false }) swalWithBootstrapButtons.fire({   title: 'Are you sure?',   text: "You won't be able to revert this!",   icon: 'warning',   showCancelButton: true,   confirmButtonText: 'Yes, delete it!',   cancelButtonText: 'No, cancel!',   reverseButtons: true }).then((result) => {   if (result.isConfirmed) {
                        swalWithBootstrapButtons.fire(
                          'Deleted!',
                          'Your file has been deleted.',
                          'success'
                        )   } else if (
                        /* Read more about handling dismissals below */
                        result.dismiss === swal.DismissReason.cancel   ) {
                        swalWithBootstrapButtons.fire(
                          'Cancelled',
                          'Your imaginary file is safe :)',
                          'error'
                        )   } }) });
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2017-08-30
                      • 1970-01-01
                      • 2021-05-23
                      • 1970-01-01
                      • 2017-01-04
                      • 2023-04-03
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多