大家晚上好
我希望您能就以下主题提供帮助。 我有这些代码行:
<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/