我需要在用户未更改 sweet alert 中的文本框中的任何值时禁用确认按钮,并且仅当文本框中的值发生更改时才启用它,但我似乎找不到方法为了这。这是我的代码:
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
或类似功能的功能?如果是,如何使用它来达到预期的结果?
这是我迄今为止所取得的成就的代码笔。
最佳答案
由于没有onInput
或类似的input: text
,您可以在onOpen
中使用getInput
并向其添加一个事件监听器以相应地启用或禁用您的按钮
。
检查工作片段。
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>
关于javascript - 如何在 sweet alert 2 中启用输入确认按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52185676/