0

为什么 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 根本不运行脚本,但它确实作为实际网页正常运行

4

2 回答 2

1

id 具有类的元素不是你的<select>. 这就是为什么。您的类<select>元素有id select

您还应该radix为函数指定参数parseInt,因为并非所有浏览器中的默认值都是10

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

小提琴

于 2013-11-09T02:44:20.540 回答
0

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

于 2013-11-09T02:44:43.067 回答