In Ordnung, so habe ich eine table
für eine Platine. Die table
wird jedes Mal aktualisiert, wenn der Spieler eine Transaktion durchführt, da die table
das Geld des Spielers verwaltet.Tabelle erstellt keine neuen Zeilen nach der vorherigen Zeile
Also, wenn der Spieler Aktien kauft, erhöht sich ein variable
namens "Gelder" um eins. Dies veranlasst die table
, die vorherige row
(dies ist $("#money" + (monies - 1) + "Tab").css("text-decoration", "line-through");
) durchzuschlagen und nach dieser row
eine neue row
($("#money" + (monies - 1) + "Tab").after('<tr id = "money' + stocks + 'Tab"><td id = "money' + stocks + '">' + moneyTotal + '</td></tr>');
) zu erstellen.
In Ordnung, also, das funktioniert gut für den Kauf von Aktien. Immer wenn jemand Aktien kauft, kreuzt er vorher die table
row
und erstellt eine neue table
row
nach. Beim Verkauf von Aktien habe ich jedoch dieselbe Formel. Es aktualisiert die "Gelder" variable
direkt vor, schlägt durch die vorherigen row
, und fügt ein weiteres hinzu. Dies funktioniert gut, aber nur das erste Mal. Wenn Sie mehr als eine Aktie verkaufen, funktioniert nicht.
meinen Code Hier ist (dies schließt den Einkauf und die Streichung von Aktien):
$("#numberPurchaseStockButton").off('click').on('click', function() {
var totalMoney = parseInt($("#purchaseStockMoneyAfterPrice").text());
if (totalMoney >= 0) {
var flipPurchaseStockAnimation = $("#purchaseStockBox");
var entireTable = $("#entireTable");
var numberPurchaseStock = $("#numberPurchaseStock").val()
var purchaseStockSelected = $("#purchaseStockSelect").val();
if (purchaseStockSelected == "Webnet") {
priceSet = webnetPrice;
}
else if (purchaseStockSelected == "Clone Corp") {
priceSet = cloneCorpPrice;
}
else if (purchaseStockSelected == "City Bank") {
priceSet = cityBankPrice;
}
else if (purchaseStockSelected == "Herbmart") {
priceSet = herbmartPrice;
}
else if (purchaseStockSelected == "Digital Systems") {
priceSet = digitalSystemsPrice;
}
else if (purchaseStockSelected == "National Airlines") {
priceSet = nationalAirlinesPrice;
}
else if (purchaseStockSelected == "State Power") {
priceSet = statePowerPrice;
}
else if (purchaseStockSelected == "Parkland") {
priceSet = parklandPrice;
}
monies++;
$("#money" + (monies - 1) + "Tab").after('<tr id = "money' + stocks + 'Tab"><td id = "money' + stocks + '">' + moneyLeft + '</td></tr>');
if (stocks >= 2) {
$("#" + (stocks - 1)).after('<tr id = "' + stocks + '" class = "yourStockTabs"><td id = "stockAmount' + stocks + '">' + numberPurchaseStock + '</td><td id = "stockType' + stocks + '">' + purchaseStockSelected + '</td><td id = "stockPurchaseCost' + stocks + '">' + cost + '</td><td id = "stockSellPrice' + stocks + '">' + priceSet + '</td></tr>');
}
else {
$("#yourStockTableHeader").after('<tr id = "' + stocks + '" class = "yourStockTabs"><td id = "stockAmount' + stocks + '">' + numberPurchaseStock + '</td><td id = "stockType' + stocks + '">' + purchaseStockSelected + '</td><td id = "stockPurchaseCost' + stocks + '">' + cost + '</td><td id = "stockSellPrice' + stocks + '">' + priceSet + '</td></tr>');
}
$("#money" + (stocks - 1) + "Tab").css("text-decoration", "line-through");
cost = 0;
entireTable.animate({
opacity: 1
}, 1000);
clearInterval(costCalculator);
flipPurchaseStockAnimation.animate({
opacity: 0
}, 1000);
setTimeout(function() {
$("#flipBlueCardBox").show();
}, 1050);
var webnetActivated = false;
var cloneCorpActivated = false;
}
else {
alert("You do not have enough money!");
}
});
});
// activate the remove stock function
var sellStockClicked = false;
$("#sellStockButton").off("click").on('click', function() {
sellStockClicked = true;
alert("Please click on the row of the stock that you would like to sell.\n\nWhen you are finished, click on the Cancel button on the top right.");
$(".yourStockTabs").css("background", "#E50000");
$(".yourStockTabs").css("cursor", "pointer");
$("#cancelButton").css("display", "block");
});
$(document).off("click").on("click", ".yourStockTabs", function() {
if (sellStockClicked === true) {
var ids = $(this).attr("id");
alert(ids);
var stockAmountIds = parseInt($("#stockAmount" + ids).text());
var stockSet = $("#stockType" + ids).text();
var webnetPrice = parseInt($("#webnetPrice").text());
var cloneCorpPrice = parseInt($("#cloneCorpPrice").text());
var cityBankPrice = parseInt($("#cityBankPrice").text());
var herbmartPrice = parseInt($("#herbmartPrice").text());
var digitalSystemsPrice = parseInt($("#digitalSystemsPrice").text());
var nationalAirlinesPrice = parseInt($("#nationalAirlinesPrice").text());
var statePowerPrice = parseInt($("#statePowerPrice").text());
var parklandPrice = parseInt($("#parklandPrice").text());
var money = parseInt($("#money" + stocks + "Tab").text());
if (stockSet == "Webnet") {
moneyBack = stockAmountIds * webnetPrice;
moneyTotal = moneyBack + money;
alert(moneyTotal);
}
else if (stockSet == "Clone Corp") {
moneyBack = stockAmountIds * cloneCorpPrice;
moneyTotal = moneyBack + money;
}
else if (stockSet == "City Bank") {
moneyBack = stockAmountIds * cityBankPrice;
moneyTotal = moneyBack + money;
}
else if (stockSet == "Herbmart") {
moneyBack = stockAmountIds * herbMartPrice;
moneyTotal = moneyBack + money;
}
else if (stockSet == "Digital Systems") {
moneyBack = stockAmountIds * digitalSystemsPrice;
moneyTotal = moneyBack + money;
}
else if (stockSet == "National Airlines") {
moneyBack = stockAmountIds * nationalAirlinesPrice;
moneyTotal = moneyBack + money;
}
else if (stockSet == "State Power") {
moneyBack = stockAmountIds * statePowerPrice;
moneyTotal = moneyBack + money;
}
else if (stockSet == "Parkland") {
moneyBack = stockAmountIds * parklandPrice;
moneyTotal = moneyBack + money;
}
monies++;
alert(monies);
$("#money" + (monies - 1) + "Tab").css("text-decoration", "line-through");
$("#money" + (monies - 1) + "Tab").after('<tr id = "money' + stocks + 'Tab"><td id = "money' + stocks + '">' + moneyTotal + '</td></tr>');
$(this).css("display", "none");
tabs++;
}
});
$("#cancelButton").click(function() {
if (sellStockClicked === true) {
sellStockClicked = false;
$(".yourStockTabs").css("background", "#FFFFFF");
$(".yourStockTabs").css("cursor", "default");
$("#cancelButton").css("display", "none");
}
});
Bitte machen Sie Ihr Codebeispiel minimal, d. H. Reduzieren Sie es auf die Methoden, die das falsche Verhalten veranschaulichen. –