如何在甜蜜警报2中的输入上启用确认按钮

问题描述 投票:3回答:1

当用户没有更改甜蜜警报内的文本框中的任何值时,我需要禁用确认按钮,并且只有当文本框中的值发生变化时才启用它,但我似乎无法找到解决方法。这是我的代码:

swal({
            title: 'Please Enter Page Name',
            input: 'text',
            inputValue: PageName,
            confirmButtonText: 'Save',
            onOpen: function (){
                swal.disableConfirmButton(); //this disables the button
            }
            preConfirm: function (Name) {
                return new Promise(function (resolve, reject) {
                    resolve();
                })
            },
            allowOutsideClick: false
        })

我使用onOpen来解雇swal.disableConfirmButton();方法,但我不知道在哪里使用swal.enableConfirmButton();。有没有像onInput或类似的功能?如果是,如何使用它来达到预期的效果?

这是迄今为止我所取得的成果。

https://codepen.io/zeeshanadilbutt/pen/NLvmZz?editors=0010

javascript jquery sweetalert sweetalert2
1个回答
3
投票

由于onInput没有input: text或类似的东西,你可以在getInput中使用onOpen并添加一个事件监听器来相应地启用或禁用你的button

检查工作代码段。

swal({
  input: 'text',
  onOpen: function () {
    swal.disableConfirmButton();
    swal.getInput().addEventListener('keyup', function(e) {
      if(e.target.value === '') {
        swal.disableConfirmButton();
      } else {
        swal.enableConfirmButton();
      }
    })
  }
});
  <script src="https://unpkg.com/sweetalert2"></script> 
© www.soinside.com 2019 - 2024. All rights reserved.