0
자바 스크립트를 사용하여 프로그램을 만들고 있는데, 버튼을 클릭하면 시트가 선택되고 배경색이 녹색으로 바뀌면서 동시에 버튼 값이 텍스트 필드와 그에 따라 토글되며 같은 기능에서 나는 버스의 다른 값 즉 운임을 전달합니다. 문제 : 좌석 버튼을 클릭하면 운임이 텍스트 상자에 추가되지만 두 개 이상의 좌석 버튼을 클릭하면 요금이 텍스트 상자에 추가되지만 운임의 합계는 적용되지 않습니다. 그리고 내가 선택한 좌석을 클릭하면 운임은 총 운임에서 공제됩니다. 여기 jquery를 사용하지 않습니다. pls 누구든지 나를 도울 수 있습니까?자바 스크립트를 사용하여 텍스트 상자에 텍스트 상자 값을 추가하는 방법
// 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;
}
}
Eval is evil -이 경우'selectedFares'의 내용에 따라 여러 가지 문제가 발생할 수 있습니다. – Jeff