gpt4 book ai didi

javascript - 如何在 Swal Sweetalert 中仅允许数字

转载 作者:太空宇宙 更新时间:2023-11-04 00:18:38 24 4
gpt4 key购买 nike

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

  {
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。这是两个不同的项目,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/

关于javascript - 如何在 Swal Sweetalert 中仅允许数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45457267/

24 4 0
文章推荐: javascript - 防止悬停在事件状态
文章推荐: html -
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com