Javascript:数量变化时更新价格

时间:2018-05-02 23:58:35

标签: javascript html price calculation

我是javascript的新手,我已经为此工作了4天了,我还没有取得任何进展。我尝试了很多不同的选择。我看到了一个像我这样的例子......但它并不适合我。 :(我试图让价格随着门票数量的变化而调整。以下是javascript及以下即html。非常感谢任何帮助!谢谢!

document.getElementById("totalTicketCost").value = 0 + "." + 00;

function ticketCost()


{

var ticketCost = 5.5;

var inputTicketQuantity = document.getElementById("inputTicketQuantity").value;

var totalTicketCost = parseFloat(ticketCost) * inputTicketQuantity;



if (!isNaN(totalTicketCost))





document.getElementById("totalTicketCost").innerHTML = totalTicketCost;



}

    <form onsubmit="" ="return alertDetails()" id="formPurchaseTickets" enctype="text/plain" method="post" action="mailto:cmst388@xyz.com">
        <h1>Ticket Purchasing Form</h1>
        <p class="alert">Act fast! This transaction must be completed in <span id="timer"></span> minutes.</p>
        <div class="field">
            <label class="required">How many tickets would you like to purchase?</label>
            <input id="inputTicketQuantity" tabindex="1" required type="number" value="0" name="ticket-quantity" min="1" max="3" step="1" title="You can only buy between 1 and 3 tickets">
            $<span id="totalTicketCost">0.00</span>
        </div>

        <div id="contactInfo" style="display:none;">
            <div class="field">
                <label class="required">Name:</label>
                <input required name="name" tabindex="2" type="text" placeholder="Enter name" pattern="[a-zA-Z\s]+" title="Enter only letters. e.g. Smith">
            </div>

            <div class="field">
                <label class="required">E-mail:</label>
                <input id="inputEmail" tabindex="3" required name="email" type="email" placeholder="Enter e-mail address"  onblur="validateEmail()">
            </div>
        </div>
        <hr>

        <input type="submit" tabindex="4" value="Purchase Tickets"> <input type="reset">
    </form>



    <script src="event_registration.js"></script>

</body>

4 个答案:

答案 0 :(得分:1)

在这里只使用ES5(假设你现阶段没有进行转换),我做了一个快速重构。

var ticketInput = document.getElementById("totalTicketCost");
var inputTicketQuantity = document.getElementById("inputTicketQuantity");
var ticketCost = 5.5;

// Handle the precision up to >= $100
function changeCost( num ) {
  var cost = new Number( parseFloat( num ) * ticketCost );
  var precision = cost.toString().length === 3 ? 3 : 4;
  return cost.toPrecision( precision );
}

inputTicketQuantity.addEventListener('input', function( event ) {
  var value = event.target.value;
  if ( !isNaN( value ) ) {
    ticketInput.innerHTML = changeCost( value );
  }
});

您肯定希望尽可能地将HTML从HTML中分离出来,避免难以阅读<form onsubmit="" ="return alertDetails()"这样的内联内容,至少应该<form onsubmit="return alertDetails()"来修复它。

答案 1 :(得分:0)

向您的数量字段添加事件监听器,并重新计算价格。

document.querySelector("#inputTicketQuantity").addEventListener("input", function() {
  let count = this.value;
  calculatePrice(count);
});

答案 2 :(得分:0)

您必须在输入数量文本框的值更改事件上调用函数ticketCost()

<input id="inputTicketQuantity" onkeydown="ticketCost()" tabindex="1" required type="number" value="0" name="ticket-quantity" min="1" max="3" step="1" title="You can only buy between 1 and 3 tickets">

或者您可以在JavaScript中添加侦听器

document.getElementById("inputTicketQuantity").addEventListener("onkeydown", ticketCost);

将其添加到JavaScript函数ticketCost()

之外

答案 3 :(得分:0)

您的问题对于您要实现的目标并不是非常具体,但这是在用户添加门票时更新总费用的功能 -

在JS中:

function updateCost(count)
{
   var ticketCost = 5.5;
   document.getElementById("totalTicketCost").innerHTML = count * ticketCost;
}

在HTML中

<input id="inputTicketQuantity" tabindex="1" required type="number" value="0" name="ticket-quantity" onchange="updateCost(this.value)" min="1" max="3" step="1" title="You can only buy between 1 and 3 tickets">