尝试将值添加到字符串,并且每次都加1

时间:2019-03-31 00:53:00

标签: javascript html css

我正在为我的大学课程创建一个在线票务预订系统,我希望能够显示用户在单击时向购物车中添加了票证,这会将票证的价值添加到每个小计中通过if语句点击最多3张票。当我使用golfTotal时,它可以工作,但是由于某种原因,当我尝试将其添加到小计中时,第一次单击时未注册,但第二次,第三次和第四次单击时都有效。

我已经尝试设置它,因此单击按钮时它不会通过if语句,而只是更改总数并将其添加到每次单击中。不确定我还能尝试什么。

<div class="golf">
    <p id="golfTotalTickets">0</p>
    <p id="golfTotal"></p>
    <p><button onclick="golfAdd();" id="golfAdd">Add to Cart</button></p>
</div>                          

<div class="subtotal">
<div class="subtotal_text">
<h3 id="subtotal">Subtotal: £ <h3 id="total"> 
0.00</h3></h3><br>
</div>
var total = 0;
var golfTotal = 0;
var golfTotalTickets = 0;

function golfAdd() {

    golfTotalTickets += 1;
    document.getElementById("total").innerHTML = total.toFixed(2);

    if (golfTotalTickets <= 1) {
    golfTotal = 15;
    total += 15;
    document.getElementById("golfTotalTickets").innerHTML = golfTotalTickets + " Ticket";
    document.getElementById("golfTotal").innerHTML = golfTotal.toFixed(2);
    } else if (golfTotalTickets == 2){
    golfTotal += 15
    total += 15
    document.getElementById("golfTotalTickets").innerHTML = golfTotalTickets + " Tickets";
    document.getElementById("golfTotal").innerHTML = golfTotal.toFixed(2);
    } else if (golfTotalTickets == 3){
    golfTotal += 15
    total += 15
    document.getElementById("golfTotalTickets").innerHTML = golfTotalTickets + " Tickets";
    document.getElementById("golfTotal").innerHTML = golfTotal.toFixed(2);
    }else{
            window.alert("You are not allowed to buy more than 3 tickets for The Open.");
    }
}

单击该按钮时,每次点击的小计的值应增加15,除非golfTotalTickets达到3时其上限为45。

1 个答案:

答案 0 :(得分:0)

您可以在计算之后将以下document.getElementById("total").innerHTML = total.toFixed(2);移动到。在您的if-else之前尚未完成计算的原因。

请参见摘要:

var total = 0;
var golfTotal = 0;
var golfTotalTickets = 0;

function golfAdd() {

    golfTotalTickets += 1;
    //document.getElementById("total").innerHTML = total.toFixed(2); //move from there to after if else

    if (golfTotalTickets <= 1) {
    golfTotal = 15;
    total += 15;
    document.getElementById("golfTotalTickets").innerHTML = golfTotalTickets + " Ticket";
    document.getElementById("golfTotal").innerHTML = golfTotal.toFixed(2);
    } else if (golfTotalTickets == 2){
    golfTotal += 15
    total += 15
    document.getElementById("golfTotalTickets").innerHTML = golfTotalTickets + " Tickets";
    document.getElementById("golfTotal").innerHTML = golfTotal.toFixed(2);
    } else if (golfTotalTickets == 3){
    golfTotal += 15
    total += 15
    document.getElementById("golfTotalTickets").innerHTML = golfTotalTickets + " Tickets";
    document.getElementById("golfTotal").innerHTML = golfTotal.toFixed(2);
    }else{
            window.alert("You are not allowed to buy more than 3 tickets for The Open.");
    }
    document.getElementById("total").innerHTML = total.toFixed(2); //here
}
<div class="golf">
    <p id="golfTotalTickets">0</p>
    <p id="golfTotal"></p>
    <p><button onclick="golfAdd();" id="golfAdd">Add to Cart</button></p>
</div>                          

<div class="subtotal">
<div class="subtotal_text">
<h3 id="subtotal">Subtotal: £ <h3 id="total"> 
0.00</h3></h3><br>
</div>