突出显示 sweetAlert 中的默认文本输入

Highlighting default text input in sweetAlert

我有一个允许文本输入的SweetAlert2,我给它一个默认值。我希望在弹出警报时突出显示此默认值,以便用户可以在需要时立即覆盖它。这是一个例子:

下面是我使用 sweetAlert 选项调用的函数:

window.sweetPrompt = function (title, message, callback, input, keepopen, allowOutsideClick, allowEscapeKey) {
    sweetAlert({
        title: title,
        text: message,
        input: 'text',
        confirmButtonColor: "#428bca",
        preConfirm: function(text) {
            return new Promise(function(resolve) {
                if (!keepopen) {
                    resolve();
                } else {
                    callback(text);
                }
            });
        },
        inputValidator: function(text) {
            return new Promise(function (resolve, reject) {
                if (text) {
                    resolve();
                } else {
                    reject('Cannot be empty!');
                }
            });
        },
        inputValue: input,
        showCancelButton: true,
        reverseButtons: true,
        allowOutsideClick: allowOutsideClick,
        allowEscapeKey: allowEscapeKey
    }).then(callback, function(dismiss){});
};

我该怎么做(如果可能的话)?我考虑过使用 jQuery 但我不确定如何获取对 sweetAlert 对话的引用。 如有任何建议,我们将不胜感激。

给你:

Swal.fire({
  input: 'text',
  inputValue: 'input value',
  didOpen: () => {
    const input = Swal.getInput()
    input.setSelectionRange(0, input.value.length)
  }
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

PS。请注意,SweetAlert2 和 SweetAlert 是两个不同的项目,在 API.

中略有不同

SweetAlert2 文档:https://sweetalert2.github.io/