我怎样才能在 Swal Sweetalert 中只允许数字

How can I allow numeric only in Swal Sweetalert

我想知道我怎么能在这部分只允许数字。

  {
    title: "Dollar Amount?",
    text: "How much is your Dollar Question worth?",
    inputPlaceholder: "Enter Amount"
  }

我正在使用 Sweetalert 插件。这几天一直困扰着我,我只是前端的新手,我真的是一个完整的后端人。

function showDollarQuestion() {

if (inUserId === "" || inUserId === null) {
    socket.emit('stud notif', myuserid,myuserid,"noroom");
}else{
    swal.setDefaults({
      input: 'text',
      confirmButtonText: 'Next →',
      showCancelButton: true,
      animation: false,
      progressSteps: ['1', '2']
    })

    var steps = [
      {
        title: "Dollar Question?",
        text: "Ask a question to your influencer",
        inputPlaceholder: "Write your Dollar Question"
      },
      {
        title: "Dollar Amount?",
        text: "How much is your Dollar Question worth?",
        inputPlaceholder: "Enter Amount"
      }
    ]

    swal.queue(steps).then(function (result) {
        if (result[1] === "" || result[1] === "") {
            swal.resetDefaults()
                swal({
                title: 'Empty Field!',
                html:
                  'Dollar Question is required<br />Dollar Amount is required',
                confirmButtonText: 'Try Again',
                showCancelButton: false
            })
        }else if(){

        }else{
            swal.resetDefaults()
            swal({
                title: 'All done!',
                html:
                  'Your Dollar Question is '+JSON.stringify(result[0]).replace(/\"/g, "")+
                  '<br /> Dollar Question worth is '+JSON.stringify(result[1]).replace(/\"/g, ""), 
                confirmButtonText: 'Great, your question has been successfully submitted to your influencer',
                showCancelButton: false
            })
            socket.emit('dollar quest', JSON.stringify(result[0]).replace(/\"/g, ""), JSON.stringify(result[1]).replace(/\"/g, ""), inUserId, myuserid, 'dquest');

        }
    }, function () {
      swal.resetDefaults()
    })
}
}

到目前为止,这是我得到的所有代码。而且我找不到任何关于这个 Sweetalert 东西的教程。提前谢谢大家

首先,您使用的是 SweetAlert2,而不是 SweetAlert。这是 2 个不同的项目,在 API.

方面略有不同

为了制作数字字段,您应该将 input 参数设置为 'number':

Swal.fire({
  text: 'How much is your Dollar Question worth?',
  input: 'number'
}).then(function(result) {
  if (result.value) {
    const amount = result.value
    Swal.fire(amount + ' USD')
  }
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

有关input参数的更多详细信息,请参阅官方文档页面:https://sweetalert2.github.io/

不过你可以换书店或者你已经习惯了,可以用

$('.showSweetAlert').children('fieldset').children('input').attr('type', 'number');

您可以使用 'text',然后在收到响应时将其格式化为数字(双精度)。

'number' 不允许 commas/points(仅限数字)并且在您删除它之前会显示警告