我无法使代码中的所有星号生效。有人可以帮忙吗?该函数工作正常,只是document.write('This will not print');
实际上不起作用。有什么问题?
// // UPDATE
首先,感谢您的快速回复。现在我将发布完整的代码。 如您所见,变量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>
答案 0 :(得分:3)
修改强>
问题在于你的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()
或简单提醒。
答案 1 :(得分:2)
这是因为循环失败
您永远不会在任何地方设置ii
。
检查您的控制台以确保没有javascript错误。