代码之家  ›  专栏  ›  技术社区  ›  kowsikbabu

选择座位时获取未定义的元素ID

  •  1
  • kowsikbabu  · 技术社区  · 8 年前
    window.onload=function()。{
    创建座位(oseattable、numseatsperrow、rownums);
    }
    
    

    然后我调用createSeats函数来创建座位。

    附件儿童(OROW); var orow=document.createElement(“tr”); var oimg=document.createElement(“img”); oimg.status='可用'; var x=dis[c]; oimg.id=座椅等级+j+(i*座椅位置行); } this.status=(this.status='可用')?'我的' } 奥罗。附属物(ocell);

    
    
    当我选择前两个座位时,我得到的座位号码是正确的,
    
    

    但是当我选择两个随机座位时,我会得到“未定义”作为元素ID seatsselected=new array(); seatsselected.push(OSEATS[i].id); var totalprice=seatsselected.length*座位价格; ototalPrice.value=总价;

    事先谢谢!

    window.onload = function() {
        ...
        createSeats(oseattable, numseatsperrow, rownums);
        ...
    }
    

    function createSeats(oSeatsContainer, seatsPerRow, rowNums){
        var oRow = document.createElement('tr');
        oRow.appendChild(document.createElement('th'));
        oSeatsContainer.appendChild(oRow);
        var seatClass = (document.getElementById('class')=="Economy")?1:11;
        for (i = 0; i < rowNums; i++) {
            var oRow = document.createElement('tr');
            for (j = 0; j < seatsPerRow; j++) {
                var oCell = document.createElement('td');
                var oImg = document.createElement('img');
                oImg.src = statusPics['avail'].src;
                oImg.status = 'avail';
                oImg.id = seatClass + j + (i*seatsPerRow);
                for (c=0; c < dis.length; c++) {
                    var x = dis[c];
                    if (seatClass + j == x) {
                        oImg.src = statusPics['unavail'].src;
                        oImg.status = 'taken';
                        oImg.id = seatClass + j + (i*seatsPerRow);
                    }
                }
                oImg.onclick = function() {
                    if (this.status != 'taken') {
                        this.status = (this.status == 'avail') ? 'mine'
                                : 'avail';
                        this.src = (this.status == 'avail') ? statusPics['avail'].src
                                : statusPics['mine'].src;
                    }
                }
                oCell.appendChild(oImg);
                oRow.appendChild(oCell);
            }
            oSeatsContainer.appendChild(oRow);
        }
    }
    

    我得到了我想要的座位布局

    当我选择前两个座位时,我得到的座位号码是正确的, first two seats

    random seatsrandom seats

    function confirmChoices() {
        seatsSelected = new Array();
        var strBookedSeats = '';
        for (i = 0; i < oseats.length; i++) {
            if (oseats[i].status == 'mine') {
                seatsSelected.push(oseats[i].id);
                oseats[i].src = statusPics['taken'].src
                if(strBookedSeats=='')
                    strBookedSeats += seatsSelected[i];
                else
                strBookedSeats += ',' + seatsSelected[i];
            }
        }
        var totalPrice = seatsSelected.length * seatPrice;
        oBookedSeatNos.innerHTML = strBookedSeats;
        oBooked.value = strBookedSeats;
        oTotalprice.value = totalPrice;
        oDispPrice.innerHTML = totalPrice;
        oBtnBookSeats.disabled = (seatsSelected.length == seatstobebooked) ? false: true;
    }
    

    我做错什么了?如何纠正?

    2 回复  |  直到 8 年前
        1
  •  2
  •   Georg.Duees    8 年前

    function confirmChoices() {
    
      // array in which you add your 'seats selected seats'
      seatsSelected = new Array();
      var strBookedSeats = '';
    
      for (i = 0; i < oseats.length; i++) {
        //the oseats array might be quite large for example at the fourth seat you check 'i' will be 3.
    
        if (oseats[i].status == 'mine') {
          // just if the status is 'mine' the id of entry will be pushed to the array.
          seatsSelected.push(oseats[i].id);
          oseats[i].src = statusPics['taken'].src
    
          //this works for the first seat as 'i' starts at 0.
          // if you change the ID to be used from the oseats array this should work.
          if (strBookedSeats == '')
              //changed seatsSelected[i] with oseats[i].id
            strBookedSeats += oseats[i].id;
          else
              //changed seatsSelected[i] with oseats[i].id
            strBookedSeats += ',' + oseats[i].id;
        }
      }
      var totalPrice = seatsSelected.length * seatPrice;
      oBookedSeatNos.innerHTML = strBookedSeats;
      oBooked.value = strBookedSeats;
      oTotalprice.value = totalPrice;
      oDispPrice.innerHTML = totalPrice;
      oBtnBookSeats.disabled = (seatsSelected.length == seatstobebooked) ? false : true;
    }
        2
  •  0
  •   Soorya J    8 年前

    由于将值推送到数组中,您得到的答案是错误的。删除中的数组

    function confirmChoices() {
            var strBookedSeats = '';
            var count = 0;
            for (i = 0; i < oseats.length; i++) {
                if (oseats[i].status == 'mine') {
                    count++;
                    console.log("After: "+oseats[i].id);
                    oseats[i].src = statusPics['taken'].src
                    if(strBookedSeats=='')
                        strBookedSeats += oseats[i].id;
                    else
                    strBookedSeats += ',' + oseats[i].id;
                }
            }
            var totalPrice = count * seatPrice;
            oBookedSeatNos.innerHTML = strBookedSeats;
            oBooked.value = strBookedSeats;
            oTotalprice.value = totalPrice;
            oDispPrice.innerHTML = totalPrice;
            oBtnBookSeats.disabled = (count == seatstobebooked) ? false: true;
        }