验证选项javascript

时间:2017-06-15 00:38:09

标签: javascript

我需要一些帮助,我知道它是一个非常简单的解决方案,但我一直卡在代码的这一部分,我需要验证我的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> 

1 个答案:

答案 0 :(得分:0)

你已经有了一段时间的循环,所以我不知道为什么你没有使用类似的逻辑来检查。

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);
}
带有url === null的{​​{1}}是允许用户在他们决定取消或w / e时实际退出无限提示循环。您也应该将它用于您的宽度/高度(尽管这取决于您)。

以下代码段:

&#13;
&#13;
return
&#13;
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;
}
&#13;
&#13;
&#13;