【问题标题】:How can I allow numeric only in Swal Sweetalert如何在 Swal Sweetalert 中只允许数字
【发布时间】:2017-08-02 09:45:26
【问题描述】:

我想知道如何在这部分只允许数字。

  {
    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 的教程。先谢谢各位了

【问题讨论】:

    标签: javascript html node.js bootstrap-4 sweetalert


    【解决方案1】:

    首先,您使用的是 SweetAlert2,而不是 SweetAlert。这是两个不同的项目,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')
      }
    })
    &lt;script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"&gt;&lt;/script&gt;

    更多input参数详见官方文档页面:https://sweetalert2.github.io/

    【讨论】:

    • 您好,先生,我怎样才能使用多个输入呢?因为我的原因我有一个类似的数组,它们都设置为默认为input:'text'
    【解决方案2】:

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

    “数字”不允许逗号/点(仅限数字),并且会显示警告,直到您将其删除

    【讨论】:

      【解决方案3】:

      但是你可以换个书店或者你已经习惯了,你可以用

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

      【讨论】:

        猜你喜欢
        • 2022-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-17
        • 2016-07-20
        • 2017-07-20
        • 1970-01-01
        • 2021-10-07
        相关资源
        最近更新 更多