javascript - 带有两个输入字段的 SweetAlert 提示

标签 javascript prompt sweetalert

目前正在从事个人项目。我希望用户单击一个按钮,系统会显示一个 SweetAlert 提示,供用户验证其凭据。但是,我在 SweetAlert 网站上看到的代码只允许一个输入字段。这是我的代码:

swal({
  title: "Authenicating for continuation",
  text: "Test",
  type: "input",
  showCancelButton: true,
  closeOnConfirm: false,
  animation: "slide-from-top",
  inputPlaceholder: "Write something"
}, function(inputValue) {
  if (inputValue === false) return false;
  if (inputValue === "") {
    swal.showInputError("You need to write something!");
    return false
  }
  // swal("Nice!", "You wrote: " + inputValue, "success");
});

那么,有没有办法让我获得两个输入字段?一个输入字段用于密码,另一个输入字段用于文本。

最佳答案

现在 SweetAlert2 可用: https://sweetalert2.github.io

根据他们在底部的信息:

Multiple inputs aren't supported, you can achieve them by using html and preConfirm parameters. Inside the preConfirm() function you can pass the custom result to the resolve() function as a parameter:

swal({
  title: 'Multiple inputs',
  html:
    '<input id="swal-input1" class="swal2-input">' +
    '<input id="swal-input2" class="swal2-input">',
  preConfirm: function () {
    return new Promise(function (resolve) {
      resolve([
        $('#swal-input1').val(),
        $('#swal-input2').val()
      ])
    })
  },
  onOpen: function () {
    $('#swal-input1').focus()
  }
}).then(function (result) {
  swal(JSON.stringify(result))
}).catch(swal.noop)

关于javascript - 带有两个输入字段的 SweetAlert 提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31463649/

相关文章:

git difftool : how to measure progress, 那是差异化和差异化的文件数量?

javascript - 链接 sweetalert2 模态

twitter-bootstrap-3 - SweetAlert 位置 : from fixed to absolute

javascript - 甜蜜警报不起作用

javascript - jquery javascript 显示/隐藏切换删除内容

javascript - 如果名称为 x,则将类添加到输入字段

shell - Powershell 中的 $ExecutionContext.SessionState.Path.CurrentLocation 和 $pwd 有什么区别?

input - perl6 有没有办法进行可编辑的提示输入?

javascript - 在没有 coffeescript 的 rails 3.2 中使用 javascript

javascript - 设置 scrollTop 跨浏览器