我需要一些帮助,我知道这是一个非常简单的解决方案,但我总是卡在代码的这一部分,我需要验证我对 1、2 或 3 的选择并访问该网站 提前致谢
对于某些人来说,我需要用户选择 1 2 或 3,用户选择 1 2 或 3 将提示他们输入高度和宽度 500,如果输入正确,则输出错误低于输出错误 转到该站点只需要它来验证选择
function redirect() {
var choice=0;
var height;
var width;
var url = Number (prompt("Pick a site? 1. xxxx 2. xxxx 3. xxx", ""));
switch (url)
{
case 1:
while(!isFinite(height) || height < 500) {
height = prompt("Enter a height > 500");
}
while(!isFinite(width) || width < 500) {
width = prompt("Enter a width > 500");
}
window.open(url="http://www.xxx", "myWindow","status = 1, height = 500, width = 500, resizable = 0");
break;
case 2:
while(!isFinite(height) || height < 500) {
height = prompt("Enter a height > 500");
}
while(!isFinite(width) || width < 500) {
width = prompt("Enter a width > 500");
}
window.open(url="http://xxx", "myWindow","status = 1, height = 500, width = 500, resizable = 0");
break;
case 3:
while(!isFinite(height) || height < 500) {
height = prompt("Enter a height > 500");
}
while(!isFinite(width) || width < 500) {
width = prompt("Enter a width > 500");
}
window.open(url="http://www.xxx", "myWindow","status = 1, height = 500, width = 500, resizable = 0");
break;
default:
alert("Please enter number 1, 2, or 3 to select a website");
return false;
}// end switch
return;
}
<button type="button" onclick="redirect()" onsubmit="return validateForm()">Go to other websites</button>
你已经有一段时间循环了,所以我不知道你为什么不直接使用类似的逻辑来检查。
var url;
while (url === undefined || [1,2,3].indexOf(url) === -1)
{
url = prompt("Pick a site? 1. xxxx 2. xxxx 3. xxx", "");
if (url === null)
{
return false;
}
url = Number(url);
}
return
url === null
是允许用户在决定取消或W/E时实际退出无限提示循环。您也应该将其用于宽度/高度(尽管这取决于您(。
以下代码段:
function redirect() {
var choice=0;
var height;
var width;
var url;
let i = 0;
while (url === undefined || [1,2,3].indexOf(url) === -1)
{
url = prompt("Pick a site? 1. xxxx 2. xxxx 3. xxx", "");
if (url === null)
{
return false;
}
url = Number(url);
}
switch (url)
{
case 1:
while(!isFinite(height) || height < 500) {
height = prompt("Enter a height > 500");
}
while(!isFinite(width) || width < 500) {
width = prompt("Enter a width > 500");
}
window.open(url="http://www.xxx", "myWindow","status = 1, height = 500, width = 500, resizable = 0");
break;
case 2:
while(!isFinite(height) || height < 500) {
height = prompt("Enter a height > 500");
}
while(!isFinite(width) || width < 500) {
width = prompt("Enter a width > 500");
}
window.open(url="http://xxx", "myWindow","status = 1, height = 500, width = 500, resizable = 0");
break;
case 3:
while(!isFinite(height) || height < 500) {
height = prompt("Enter a height > 500");
}
while(!isFinite(width) || width < 500) {
width = prompt("Enter a width > 500");
}
window.open(url="http://www.xxx", "myWindow","status = 1, height = 500, width = 500, resizable = 0");
break;
default:
break;
}// end switch
return;
}
<button type="button" onclick="redirect()" onsubmit="return validateForm()">Go to other websites</button>