IF返回假

时间:2013-11-09 02:33:16

标签: javascript

为什么第28行和第39行的if(if( clas == 1)if( clas ==2 )总是会返回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/ 附: jsfiddle根本不运行脚本,但它确实作为实际网页正常运行

2 个答案:

答案 0 :(得分:1)

id 的元素不是您的<select>。这就是为什么。您的班级<select>元素已id 选择

此外,您应该为radix功能指定parseInt参数,因为所有浏览器中的默认值都不是 10

//and do not forget the var statement if you want a local variable
var clas = parseInt(cla, 10); 

FIDDLE

答案 1 :(得分:0)

我放了document.getElementById('class').value,但相应的元素实际上是document.getElementById('select')