Javascript代码未运行 [英] Javascript code not running

查看:57
本文介绍了Javascript代码未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

好​​吧我放入了一个javascript代码,它一开始看起来很正确,但是当我尝试使用它失败了,如果你能看到我甚至尝试将这两个放在不同的javascript文件中但它只能只阅读其中一个。



hello
well i put in a javascript code and it looked quite right at first but when i try to use it it fails and if you can see i even tried putting these two in different javascript files but still it only read only one of them.

function setValue(){
myVariable= document.forms["myform"]["gname"].value;
myVariable2= document.forms["myform"]["gpc"].value;
myVariable3= document.forms["myform"]["procesor"].value;
myVariable4= document.forms["myform"]["ram"].value;
myVariable5= document.forms["myform"]["os"].value;
var gname = ["Prince of Persia: The Forgotten Sands"];
var gpc = ["GeForce 8600 GTS 512MB", "GeForce 8800 Ultra", "GeForce 8800 GTX", "GeForce 8800 GTS (640 MB)", "GeForce 8800 GTS (512 MB)", "GeForce 8800 GTS (320 MB)", "GeForce 8800 GT", "GeForce 8800 GS", "GeForce 8600 GTS", "9800 GX2 (1 GB)", "9800 GTX (512MB GDDR3)", "9800 GTX+ (512MB GDDR3)", "9800 GT(512MB GDDR3)", "9600 GT(512MB)", "9600 GSO (384MB)", "9500 GT (256MB GDDR3)", "GeForce GTX 590", "GeForce GTX 580", "GeForce GTX 570", "GeForce GTX 560", "GeForce GTX 560", "GeForce GTX 550", "GeForce GT 520", "GeForce GTX 560" , "GeForce GTX 560" , "GeForce GTX 555" , "GeForce GT 545 GDDR5" , "GeForce GT 545 DDR3" , "GeForce GT 530" , "GeForce GT 520" , "GeForce 510" , "GeForce GTX 400 GPUs" , "GeForce GTX 480" , "GeForce GTX 470" , "GeForce GTX 460" , "GeForce GTX 465" , "GeForce GTS 450" , "GeForce GT 440" , "GeForce GT 430" , "GeForce GTX 460" , "GeForce GTS 450" , "GeForce GT 440" , "GeForce GT 430" , "GeForce GT 420" , "GeForce 405" , "GeForce GT 340" , "GeForce GT 330" , "GeForce GT 320","GeForce 315"];
var procesor = ["Core 2 Duo E4500 2.2GHz", "Athlon 64 X2 Dual Core 4400+"];
var ram = ["3 GB", "2 GB", "1 GB"];
var os = ["Windows Xp", "Windows 7", "Windows Vista", "Windows 8"];
var canRun = false;
 {
for(i=0;i<gname.length;i++)
        if (myVariable === gname[i]) //changed from 0 to i here
        {   
for (j=0;j<gpc.length;j++)
    {
        if (myVariable2 === gpc[j]) //changed from 0 to i here
        {
for (k=0;k<procesor.length;k++)
    {
        if (myVariable3 === procesor[k]) //changed from 0 to i here
        {  
for (h=0;h<ram.length;h++)
    {
        if (myVariable4 === ram[h]) //changed from 0 to i here
        {  
for (l=0;l<os.length;l++)
    {
        if (myVariable5 === os[l]) //changed from 0 to i here
{   
            canRun = true;  
        }
    }
}
}
}
}
}
}
}
}

    if (canRun)
    {   
        alert("yes this game can run");
    }
    else 
    {       
        alert("No, This game cannot run");
    }
};


function setValue(){

myVariable= document.forms["myform"]["gname"].value;
myVariable2= document.forms["myform"]["gpc"].value;
myVariable3= document.forms["myform"]["procesor"].value;
myVariable4= document.forms["myform"]["ram"].value;
myVariable5= document.forms["myform"]["os"].value;
var a = ["Gears of War"];
var b = ["ATI Radeon 5000 Series", "ATI Radeon 4000 Series", "ATI Radeon 3000 Series", "ATI Radeon HD 2900 Overview", "ATI Radeon HD 2600 Overview", "ATI Radeon HD 2400 Overview", "ATI Radeon X1950 Series", "ATI Radeon X1900 Series", "ATI Radeon X1800 Series", "ATI Radeon X1650 Series", "ATI Radeon X1600 Series", "ATI Radeon X1550 Series", "ATI Radeon X1300 Series", "ATI Radeon X1050 Series"];
var c = ["Core 2 Duo E4500 2.2GHz", "Athlon 64 X2 Dual Core 4400+"];
var d = ["3 GB", "2 GB", "1 GB"];
var e = ["Windows Xp", "Windows 7", "Windows Vista", "Windows 8"];
var canRun = false;
for(m=0;m<a.length;m++)
{
        if (myVariable === a[m]) //changed from 0 to i here
        {   
for (n=0;n<b.length;n++)
    {
        if (myVariable2 === b[n]) //changed from 0 to i here
        {
for (o=0;o<c.length;o++)
    {
        if (myVariable3 === c[o]) //changed from 0 to i here
        {  
for (p=0;p<d.length;p++)
    {
        if (myVariable4 === d[p]) //changed from 0 to i here
        {  
for (q=0;q<e.length;q++)
    {
        if (myVariable5 === e[q]) //changed from 0 to i here
{   
            canRun = true;  
        }
        
    }
}
}
}
}
}
}
}
}

if (canRun)
    {   
        alert("yes this game can run");
    }
    else 
    {       
        alert("No, This game cannot run");
    }
};

推荐答案

只是对您使用的逻辑进行快速评论 - 如果应该有的话如果满足所有条件就会发生,如果不满足这些条件中的任何一个就不会发生。



即 - 通常的做法是设置一个条件变量首先然后在不满足条件时改变它 - 它比所有那些嵌套的/ if循环更好读。



例如

Just a quick comment on the logic you've used - if something should happen if all of the conditions are met, then it shouldn't happen if a single one of those conditions is not met.

I.e - It's common practise to set a variable first then alter it if a condition isn't met - it's much nicer to read than all those nested for/if loops.

E.g
// returns true if all conditions are met
// false otherwise
function checkConditions()
{
	var isOk = true;
	var a=1, b=1, w=2, x=2, y=3, z=4;
	
	if (a != b)
		isOk = false;
		
	if (w != x)
		isOk = false;
		
	if (y != z)
		isOk = false;
	
	return isOk;
}

// returns true if all conditions are met
// false otherwise
// modified to exit as soon as a condition fails - this may save much or little time, it depends on the complexity of the conditions
function checkConditions()
{
//	var isOk = true;
	var a=1, b=1, w=2, x=2, y=3, z=4;
	
	if (a != b)
		return false;	//isOk = false;
		
	if (w != x)
		return false;	//isOk = false;
		
	if (y != z)
		return false;	//isOk = false;
	
	return true;
}


这是我自己修理它的方式



here is how i myself fixed it

function setValue(){
myVariable= document.forms["myform"]["gname"].value;
myVariable2= document.forms["myform"]["gpc"].value;
myVariable3= document.forms["myform"]["procesor"].value;
myVariable4= document.forms["myform"]["ram"].value;
myVariable5= document.forms["myform"]["os"].value;
var gname = ["Prince of Persia: The Forgotten Sands"];
var gpc = ["GeForce 8600 GTS 512MB", "GeForce 8800 Ultra", "GeForce 8800 GTX", "GeForce 8800 GTS (640 MB)", "GeForce 8800 GTS (512 MB)", "GeForce 8800 GTS (320 MB)", "GeForce 8800 GT", "GeForce 8800 GS", "GeForce 8600 GTS", "9800 GX2 (1 GB)", "9800 GTX (512MB GDDR3)", "9800 GTX+ (512MB GDDR3)", "9800 GT(512MB GDDR3)", "9600 GT(512MB)", "9600 GSO (384MB)", "9500 GT (256MB GDDR3)", "GeForce GTX 590", "GeForce GTX 580", "GeForce GTX 570", "GeForce GTX 560", "GeForce GTX 560", "GeForce GTX 550", "GeForce GT 520", "GeForce GTX 560" , "GeForce GTX 560" , "GeForce GTX 555" , "GeForce GT 545 GDDR5" , "GeForce GT 545 DDR3" , "GeForce GT 530" , "GeForce GT 520" , "GeForce 510" , "GeForce GTX 400 GPUs" , "GeForce GTX 480" , "GeForce GTX 470" , "GeForce GTX 460" , "GeForce GTX 465" , "GeForce GTS 450" , "GeForce GT 440" , "GeForce GT 430" , "GeForce GTX 460" , "GeForce GTS 450" , "GeForce GT 440" , "GeForce GT 430" , "GeForce GT 420" , "GeForce 405" , "GeForce GT 340" , "GeForce GT 330" , "GeForce GT 320","GeForce 315"];
var procesor = ["Core 2 Duo E4500 2.2GHz", "Athlon 64 X2 Dual Core 4400+"];
var ram = ["3 GB", "2 GB", "1 GB"];
var os = ["Windows Xp", "Windows 7", "Windows Vista", "Windows 8"];
var canRun = false;
 {
for(i=0;i<gname.length;i++)
        if (myVariable === gname[i]) //changed from 0 to i here
        {
for (j=0;j<gpc.length;j++)
    {
        if (myVariable2 === gpc[j]) //changed from 0 to i here
        {
for (k=0;k<procesor.length;k++)
    {
        if (myVariable3 === procesor[k]) //changed from 0 to i here
        {
for (h=0;h<ram.length;h++)
    {
        if (myVariable4 === ram[h]) //changed from 0 to i here
        {
for (l=0;l<os.length;l++)
    {
        if (myVariable5 === os[l]) //changed from 0 to i here
{
            canRun = true;
        }
    }
}
}
}
}
}
}
}
}


var a = ["Gears of War"];
var b = ["ATI Radeon 5000 Series", "ATI Radeon 4000 Series", "ATI Radeon 3000 Series", "ATI Radeon HD 2900 Overview", "ATI Radeon HD 2600 Overview", "ATI Radeon HD 2400 Overview", "ATI Radeon X1950 Series", "ATI Radeon X1900 Series", "ATI Radeon X1800 Series", "ATI Radeon X1650 Series", "ATI Radeon X1600 Series", "ATI Radeon X1550 Series", "ATI Radeon X1300 Series", "ATI Radeon X1050 Series"];
var c = ["Core 2 Duo E4500 2.2GHz", "Athlon 64 X2 Dual Core 4400+"];
var d = ["3 GB", "2 GB", "1 GB"];
var e = ["Windows Xp", "Windows 7", "Windows Vista", "Windows 8"];
for(m=0;m<a.length;m++)
{
        if (myVariable === a[m]) //changed from 0 to i here
        {
for (n=0;n<b.length;n++)
    {
        if (myVariable2 === b[n]) //changed from 0 to i here
        {
for (o=0;o<c.length;o++)
    {
        if (myVariable3 === c[o]) //changed from 0 to i here
        {
for (p=0;p<d.length;p++)
    {
        if (myVariable4 === d[p]) //changed from 0 to i here
        {
for (q=0;q<e.length;q++)
    {
        if (myVariable5 === e[q]) //changed from 0 to i here
{
            canRun = true;
        }

    }
}
}
}
}
}
}
}
}

if (canRun)
    {
        alert("yes this game can run");
    }
    else
    {
        alert("No, This game cannot run");
    }
};


这篇关于Javascript代码未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆