gpt4 book ai didi

javascript - do-while 循环和 window.alert 消息

转载 作者:行者123 更新时间:2023-11-30 20:09:26 24 4
gpt4 key购买 nike

大家晚上好

我希望您能就以下主题提供帮助。我有这些代码行:

<script>
function save3() {
var pn = 4;
var flag = true;
do {
var selection = window.prompt("Give the User Id:", "Type a number!");
if (selection == (/^[0-9.,]+$/)) {
flag = false;
window.alert("ok");
}
} while (flag != false);
$("#user_id").val(selection)
//$("#user_id").val(prompt("Give the User Id:"))
do {
var selection2 = parseInt(window.prompt("Give the Book Id:", "Type a number!"), 10);
} while (isNaN(selection2));
$("#book_id").val(selection2)
//$("#book_id").val(prompt("Give the Book Id:"))
do {
var selection3 = parseInt(window.prompt("Give the Game Id:", "Type a number!"), 10);
} while (isNaN(selection3));
$("#game_id").val(selection3)
//$("#game_id").val(prompt("Give the Game Id:"))
$("#site_id").val(pn)
}

function load2() {

}
</script>

我的问题对你来说可能很简单,但我想不出解决方案。我希望用户向 window.prompt 框中插入一个值。如果值是一个数字。我想向用户显示一条消息“正确”。如果一个值是一个字母或其他东西,我想显示一条消息“再试一次”并且应该再次执行循环。

你能为我提供一些帮助吗?我认为我已经完成了我想要的一部分......

最佳答案

if (selection == (/^[0-9.,]+$/)){

这不是您使用正则表达式的方式。您需要使用 match()test()

if (selection && selection.match(/^[0-9.,]+$/))
if (selection && /^[0-9.,]+$/.test(selection))

关于javascript - do-while 循环和 window.alert 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52555363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com