简体   繁体   English

如何使用javascript在文本框中添加文本框值

[英]How to add the textbox value inside the textbox using javascript

I am creating a program using javascript while a clicking of button it will select a seat and change its background color to green and at the same time the button value will be added to the text field and will toggle accordingly and in the same function i am passing a another value ie fare of the bus. 我正在使用javascript创建一个程序,同时单击按钮将选择一个座位并将其背景颜色更改为绿色,同时按钮值将添加到文本字段中,并将相应地切换,并且在相同的功能中传递另一个值,即公共汽车的票价。 Issue:When i click a seat button its fare is adding to the textbox, but if i will click two or more seat button the fare is just added to the textbox but not doing the sum of the fare. 问题:当我单击一个座位按钮时,其票价将添加到文本框中,但是如果我将单击两个或多个座位按钮,则该票价只会添加到文本框中,而不是总和。 and again when i click a selected seat the fare will be deducted from the total fare. 当我单击一个选定的座位时,票价将从总票价中扣除。 Here I am not using jquery. 在这里我不使用jQuery。 pls can anybody help me? 请问有人可以帮助我吗?

// Create a variable for the array!
var selectedSeats = new Array();
var selectedFares= new Array();

// Build a function that will update the textfield.
// Call this, whenever the array gets changed!
function updateListOfSelectedSeats() {
    document.getElementById('selectedseat').value = selectedSeats.join(',');
    document.getElementById('selectedfare').value = selectedFares;
}

// Removes a seat from the list of selected seats
function removeSeatFromList(seat,seatFare) {
    for (var i = 0; i < selectedSeats.length; i++) {
        if (selectedSeats[i] == seat) {
            selectedSeats.splice(i, 1);
            updateListOfSelectedSeats();
            removeFareFromList(seatFare);
            break;
        }
    }
}
function removeFareFromList(seatFares) {
  for (var i = 0; i < selectedFares.length; i++) {
    if (selectedFares[i] == seatFares) {
        selectedFares.splice(i, 1);
        updateListOfSelectedSeats();
        break;
    }
  }

} }

// Now the function that reacts to clicks on a seat
function setId(id, value,fare) {
    var Seat = document.getElementById(id);

    switch (Seat.style.backgroundImage) {
        case 'url("themes/frontend/images/greenseat.png")':
            // Seat is already selected and needs to be deselected
            Seat.style.backgroundImage = 'url("themes/frontend/images/seat.png")';
            removeSeatFromList(value,fare);
            break;

        case '':
        case 'url("themes/frontend/images/seat.png")':
            // Seat is empty, select it!
            Seat.style.backgroundImage = 'url("themes/frontend/images/greenseat.png")';
            selectedSeats.push(value);
            selectedFares.push(fare);
            updateListOfSelectedSeats();
            break;
    }
}

So if i understand you correctly , the text field for the price is appearing as a comma separated array instead of getting the sum of values , if that's the case what you can do is as follows : 因此,如果我对您的理解正确,那么价格的文本字段将显示为逗号分隔的数组,而不是获取值的总和,如果是这种情况,您可以执行以下操作:

document.getElementById('selectedfare').value = eval(selectedFares.join('+'));

this should get the sum of you selectedFares array . 这应该得到您的selectedFares数组的总和。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM