为什么 if 在第 28 行和第 39 行(if( clas == 1)
并且if( clas ==2 )
这个 javascript 总是返回 false?如果我用 else 替换第二个。它每次都会执行。我都试过了==
,===
我也试过把 1 和 2 作为字符串和无论有无clas = parseInt(cla)
// JavaScript Document
var seat = new Array()
var fclass
var econ
var flight
var totseats
function init( seats, firclass, econo, flightnum )
{
seat[0] = 'crew'
fclass = firclass
econ = econo
flight = flightnum
totseats = seats
for( var count = 1; count <= seats; count++ )
{
seat[count] = 0;
};
};
function reservation()
{
var cla = document.getElementById('class').value;
var name = document.getElementById('first').value;
name += ' ';
name += document.getElementById('last').value;
clas = parseInt(cla)
if( clas == 1 )
{
for( var seatnum = 1; seatnum < econ; seatnum++)
{
if( seat[seatnum] == 0 )
{
seat[seatnum] = name
break;
}
}
}
if( clas == 2 )
{
for( var seatnum = econ; seatnum < totseats; seatnum++)
{
if( seat[seatnum] == 0 )
{
seat[seatnum] = name
break;
}
}
};
pticket( name, seatnum );
};
function pticket( name, seatnum )
{
document.getElementById('tname').innerHTML = name;
document.getElementById('flight').innerHTML = 'Flight: ' + flight;
document.getElementById('seat').innerHTML = seatnum;
if( seatnum < econ )
{
document.getElementById('class').innerHTML = 'First Class';
}
else
{
document.getElementById('class').innerHTML = 'Economy';
}
};
http://jsfiddle.net/FB26t/1/ ps jsfiddle 根本不运行脚本,但它确实作为实际网页正常运行