-3

我正在尝试创建这个程序,以便当您单击按钮 1 时,它将运行函数 firstClass。不太清楚为什么它不起作用。

HTML:

  <form onSubmit="return firstClass();">

  <h1>Click "1" for first class seats. Click "2" for economy class seats</h1>

     <button type="submit">1</button>
  </form>

Javascript:

 var seatArray = [ , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
 var first = 1;
 var economy = 6;

        function firstClass()
        {               
           if (first <= 5 && seatArray == 0) 
           {    
              window.alert("You have chosen First Class. You have been given seat number: ");
           }
           else
           {
              window.alert("Seats are full");
           }
           return false;
        }
4

1 回答 1

2

你的函数被调用。只将方法设为全局,因为 fiddle 在另一个范围内运行它。

 var seatArray = [ , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
 var first = 1;
 var economy = 6;

    window.firstClass = function()
    {               
       if (first <= 5 && seatArray == 0) 
       {    
          window.alert("You have chosen First Class. You have been given seat number: ");
       }
       else
       {
          window.alert("Seats are full");
       }
       return false;
    }

http://jsfiddle.net/fvAAG/

于 2013-11-13T16:57:37.923 回答