簡體   English   中英

for循環完成后,JavaScript無法正常工作

[英]Javascript not working after for loop is done

我無法使代碼中的所有星號都起作用。 有人可以幫忙嗎? 該函數工作正常,只是document.write('This will not print'); 那實際上是行不通的。 問題是什么?

// //更新

首先,感謝您的快速答復。 現在,我將發布完整代碼。 如您所見,變量ii在主體中聲明。

完整代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
<!--
function runThis() {
var fruta01 = document.getElementById('fruta').value;
var color01 = document.getElementById('color').value;
var problemo = true;
    for (newCount = 0; newCount<=ii; newCount++){
            if (storeArray[newCount].match(fruta01) && storeArray[newCount].match(color01)){
            document.write(storeArray[newCount]+'<br/>');
            problemo = false;
            };
    };
document.write('This doesnt print')
};

//-->
</script>
</head>

<body>
<script type="text/javascript">
<!--

//Matriz total
var storeArray = new Array ()

//Numero de objetos
var ii = 0;

//Color de los objetos
var colorCounter = 0
var color = new Array ()
color[0] = 'verde';
color[1] = 'amarillo';

//tipo de objetos
var objectCounter = 0
var object = new Array ()
object[0] = 'limones';
object[1] = 'platanos';
object[2] = 'manzanas';



for (ii; ii<8; ii++) {
storeArray[ii] = 'Tengo ' + ii + ' ' + object[objectCounter] +' de color ' + color[colorCounter];

        if (objectCounter != 2) {
        ++objectCounter
        }else{
        objectCounter = 0   
        }
        if (colorCounter != 1) {
        ++colorCounter
        }else{
        colorCounter = 0    
        }
} 

//print array so that you may see what you can choose...
for(var i=0;i<storeArray.length;i++){
document.write(storeArray[i]+"<br>");
}

//-->
</script>

<form>
Que fruta buscas? <input type="text" size="10" id='fruta'/><br />
De que color? <input type="text" size="10" id='color'/><br />
<input type="submit" value="Submit!" onclick="runThis()"/>
</form>

</body>
</html>

編輯

問題出在您的for循環中

for (newCount = 0; newCount <= ii; newCount++ )

應該

for (newCount = 0; newCount < ii; newCount++ )

您不必在函數后加上分號,也不必在“ if”和“ for”循環之后加上分號。

這是您代碼的更好版本

function runThis () {
    var fruta01 = document.getElementById('fruta').value;
    var color01 = document.getElementById('color').value;
    var problemo = true;

    for (newCount = 0; newCount <= ii; newCount++ )
    {
        if (storeArray[newCount].match(fruta01) && storeArray[newCount].match(color01) )
        {
            document.write(storeArray[newCount]+'<br/>');
            problemo = false;
        }
    }

    document.write('This will not print'); //**************************
}

如果要檢查,只需使用console.log()或簡單的警報即可。

那是因為循環失敗

您永遠不會在任何地方設置ii

檢查您的控制台,確保沒有JavaScript錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM