简体   繁体   English

使用Javascript更新价格总计

[英]Updating price total with Javascript

I am helping out with a booking form at the moment for a local antiquing event, but I am having trouble updating the total on the page whenever a user makes a change. 我正在为当地的古董活动提供预订表格,但是无论何时用户进行更改,我都无法更新页面上的总数。

Background: There are two ticket tiers, Premium(£10) and Regular(£5). 背景:有两个票层,Premium(10英镑)和Regular(5英镑)。 A user can also add additional "valuations" at £3 each. 用户还可以以每个3英镑的价格添加额外的“估值”。

At the moment, the user can select their ticket with a radio button, one for Premium, one for Regular. 此时,用户可以使用单选按钮选择他们的票证,一个用于Premium,一个用于常规。

They specify additional valuations with a text box. 它们使用文本框指定其他评估。

I'd like to be able to update the total whenever the user makes a change (Selects a radio button, updates the total in the textbox). 我希望能够在用户进行更改时更新总计(选择单选按钮,更新文本框中的总计)。

Unfortunately Javascript isn't my forté, so I don't know the ins and outs of the events and whether or not this is even possible. 不幸的是,Javascript不是我的强项,所以我不知道事件的来龙去脉,以及这是否有可能。 Here is my javascript so far (There is no validation yet, purely working on function at the moment) 这是我到目前为止的javascript(目前还没有验证,纯粹是在处理函数)

Javascript Function (UpdateTotal) Javascript函数(UpdateTotal)

function UpdateTotal() {
/* Get prices and calculate total */
var ticketPrice = document.getElementById('tick_choice').value;
var additionals = parseInt(document.getElementById('additional_valuations').text, 10);
var total = ticketPrice + additionals;

/* Attempt to update the value (Inside a TD tag) */
document.getElementById('totalPayable').innerHTML="£"+total;
}

What would be the best way to call this code using JS events? 使用JS事件调用此代码的最佳方法是什么?

Much appreciated! 非常感激!

EDIT : Here is the relevant HTML 编辑 :这是相关的HTML

For the radio buttons: 对于单选按钮:

<input id="tick_choice" type=RADIO VALUE="10"> Premium
<input id="tick_choice" type=RADIO VALUE="5"> Regular

For the text box: 对于文本框:

<input type="text" id="regular_tickets">

Your current html is invalid: you're not supposed to give more than one element the same id, and so when you use document.getElementById("tick_choice") it will find only one of them. 您当前的html无效:您不应给一个以上的元素相同的ID,因此,当您使用document.getElementById("tick_choice")它只会找到其中之一。 For radio buttons to function as a group they need to have the same name attribute, but they can have different ids if you want to access them with document.getElementById() . 要使单选按钮用作组,它们需要具有相同的名称属性,但如果要使用document.getElementById()访问它们,它们可以具有不同的ID。 You can make it like this: 你可以这样做:

<input id="tick_choice_premium" name="tick_choice" type=RADIO VALUE="10"> Premium
<input id="tick_choice_regular" name="tick_choice" type=RADIO VALUE="5"> Regular

<script>
   var choicePremium = document.getElementById("tick_choice_premium"),
       choiceRegular = document.getElementById("tick_choice_regular"),
       additionals = document.getElementById("additional_valuations");

   choicePremium.onclick = UpdateTotal;
   choiceRegular.onclick = UpdateTotal;
   additionals.onchange = UpdateTotal;

   function UpdateTotal() {
      /* Get prices and calculate total */
      var ticketPrice = choicePremium.checked ? choicePremium.value
                                              : choiceRegular.value;
      var additionals = parseInt(additionals.value, 10);
      var total = ticketPrice + additionals;

      /* Attempt to update the value (Inside a TD tag) */
  document.getElementById('totalPayable').innerHTML="£"+total;
   }
</script>

Put the script block at the bottom of the body, or after the elements anyway, so that the elements will have already been parsed when the code runs to assign the event handlers. 将脚本块放在正文的底部,或者在元素之后,以便在代码运行以分配事件处理程序时已经解析了元素。

What the above is doing is starting out by keeping references to the DOM elements in variables so that they can be accessed both to set the event handlers and within your function without repeating document.getElementById() everywhere. 上面所做的是从在变量中保留对DOM元素的引用开始,以便可以在设置事件处理程序和函数内对其进行访问,而无需在任何地方重复document.getElementById()

Then it sets it so when the radio buttons are changed your function will be called (for radio or check buttons the click event occurs even when they're changed via the keyboard), and also when the text box is changed your function will be called. 然后对其进行设置,以便在单选按钮更改时将调用您的功能(对于单选按钮或选中按钮,即使通过键盘更改了单击事件,也会发生单击事件),并且在文本框更改时也会调用您的功能。 Note that .onclick = UpdateTotal does not put parentheses after UpdateTotal because it is not calling the function, it is getting a reference to it so it will be called later when the event occurs. 需要注意的是.onclick = UpdateTotal 把括号后UpdateTotal ,因为它没有调用该函数,这是得到它的引用,以便在事件发生时它会被以后调用。

Then in your function it checks whether the first radio is checked and if so uses its value otherwise uses the other radio's value - obviously this works only while there are only two of them, but I'm sure you can see how this could be extended to allow for more. 然后在您的函数中检查是否检查了第一个无线电,是否使用了它的值,否则使用了另一个无线电的值-显然,这仅在只有两个无线电的情况下有效,但是我敢肯定,您可以看到如何扩展它允许更多。

Against each radio element that affects the total you can just handle "onclick": 对于影响总数的每个无线电元素,您只需处理“onclick”:

<input id="tick_choice" type="radio" value="10" onclick="UpdateTotal();" />

For a textbox you don't want to do this (it would if you clicked to enter) so instead you can look at either onchange or onkeyup 对于一个文本框,你不想这样做(如果你点击进入就会这样),所以你可以看看onchange或onkeyup

<input type="text" onchange="UpdateTotal();" />

Then your function can do the calculations as required 然后您的函数可以根据需要进行计算

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

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