for循环在js中不起作用

时间:2015-06-07 14:01:27

标签: javascript function

SelectSeat函数没有做任何事情..无法想象我们为什么......我从html onclick事件中调用它。

<html>

<head><link rel="stylesheet" type="text/css" href="mandigo.css"> 
<center> <b><i><font size="25" color="Blue"> Ticket Booking </b></i>  </font>
<script>

    var seat = [true,false,true,false,false,false];

    var sel_seat=-1;//for selecting a seat

    function setHtml(){

        alert("check");
        for(var i=0;i<seat.length ;i++){
        if(seat[i]){

            document.getElementById("seat"+(i+1)).src ="Avail.png";
            document.getElementById("seat"+(i+1)).alt ="Available";             
        }

        else{

            document.getElementById("seat"+(i+1)).src ="UnAvail.png";
            document.getElementById("seat"+(i+1)).alt ="UnAvailable";               

        }
    }
}

    function SelectSeat(){

        setHtml();

        for(var i=0;i<seat.length;i++){

            if(seat[i]){
                sel_seat=i+1;
                var accept=confirm("Seat "+(i+1)+" is Available. Book it ??");
                if(accept){
                    Booking();// in Booking change the seat color using anothr picture.
                    break;//to break out of the loop
                }
                //if not true loop continues until it fins an empty seat
                /*if(!accept){

                }*/

                if(sel_seat==-1&&i>=seat.length+1)
                    alert("No Seats Are Available");
            }

        }

    }

    function Booking(){

        document.getElementById("seat"+sel_seat).src="SelectedSeat.png";

    }





</script>
  </head>

<body>

<div><pre>

    <img  src="Avail.png" alt="Available" id="seat1"width="90" height="90"><img  src="Avail.png" alt="Available" id="seat2"width="90" height="90">
    <img  src="Avail.png" alt="Available" id="seat4"width="90" height="90"><img  src="Avail.png" alt="Available" id="seat5"width="90" height="90">
    <img  src="Avail.png" alt="Available" id="seat3"width="90" height="90"><img  src="Avail.png" alt="Available" id="seat6"width="90" height="90">
</pre></div>


<center>

    <input type="Button" value="Find Me a Seat" onClick="SelectSeat();"/>

</body>

</html>

好吧..抱歉,之前应该从一开始就给出了完整的pgm  通过不工作......我的意思是没有任何反应..没有任何错误消息...

1 个答案:

答案 0 :(得分:2)

您永远不会调用函数SelectSeat()

要运行此功能,您必须“有效”。不知何故,例如

页面加载时

window.onload=function(){SelectSeat()};

或点击某些内容:<div onclick="SelectSeat()">click me</div>';