出不来,求大神告知原因,本人刚自学,遇到问题实在不知道错在哪
<head>
<script>
var seat=[true,false,true,false,false];
function initSeats(){
for (var i=0;i<seat.length;i++){
if(seat[i]){
document.getElementById("seat"+i).src="true.jpg";
document.getElementById("seat"+i).alt="可用座位"
}
else{
document.getElementById("seat"+i).src="false.jpg";
document.getElementById("seat"+i).alt="已被选择的座位"
}
}
var selseat=i+1;
function findseat(){
if (selseat>=0) {selseat=-1;initSeats()};
for(var i=0;i<seat.length;i++){
if(seat[i]){selseat=i;
document.getElementById("seat"+i).src="true.jpg";
document.getElementById("seat"+i).alt="你的座位";
var accept=confirm("seat"+(i+1)+"是可用的,选择它?");
if(!accept){selseat=-1;
document.getElementById('"seat"+i').src="false.jpg";
document.getElementById('"seat"+i').alt="该座位已被选择";
}
}
}
};
}
</script>
</head>
<body onload="initSeats();">
<div style="margin-top:75px; text-align:center">
<img style="width:100px;height:100px;" id="seat0" src="" alt="" />
<img style="width:100px;height:100px;" id="seat1" src="" alt="" />
<img style="width:100px;height:100px;" id="seat2" src="" alt="" />
<img style="width:100px;height:100px;" id="seat3" src="" alt="" />
</div>
<br>
<button onClick="findseat()">查找座位</button>
</body>
我也在自学jsp 你把报错给贴一下 咱们一起讨论下
var selseat=i+1;
i是那来的
贴下报错看看,是啥样的
var selseat=i+1;
这个i都没定义,你要是想引用上面for循环的局部变量i的话要声明一个全局变量 var curIndex; 然后在for循环里面每次 curIndex = i;
var selseat = curIndex;
还有你的问题在于应该规范编码,注意代码的缩进,让代码可读性更高,才能更加节省开发和调试的时间
var seat=[true,false,false,false]; var i=0; function initSeats(){ for (i;i<seat.length;i++){ if(seat[i]){ document.getElementById("seat"+i).src="true.png"; document.getElementById("seat"+i).alt="可用座位" } else{ document.getElementById("seat"+i).src="false.png"; document.getElementById("seat"+i).alt="已被选择的座位" } } } var selseat=i+1; function findseat(){ if (selseat>=0) {selseat=-1;initSeats()}; for(var i=0;i<seat.length;i++){ if(seat[i]){selseat=i; document.getElementById("seat"+i).src="true.png"; document.getElementById("seat"+i).alt="你的座位"; var accept=confirm("seat"+(i+1)+"是可用的,选择它?"); if(!accept){selseat=-1; document.getElementById('"seat"+i').src="false.png"; document.getElementById('"seat"+i').alt="该座位已被选择"; } } } };我修改了一下,本地运行出来了,大概修改了三个地方,我图发给你
不太懂要干嘛,就是找没有人的位置就问下愿不愿意选这个位置。