JS自动计算多个字段的总数

用户3361789

我查看了多个用于自动计算总数的 js 示例,但没有找到任何适用于我的示例,其中包含计算不同数字的字段以组合起来创建一个总数。下面是我的代码,以便您可以了解更多。

<html>
<head>
<meta charset="utf-8">
<title>Calculator</title>

<script language="javascript">
   function Calc(className){
      var elements = document.getElementsByClassName(className);
      var totalCost = 0;
	  
      for(var i = 0; i < elements.length; i++){
         totalCost += parseInt(elements[i].value);
      }
      
      document.tickets.totalCost.value = '£' + totalCost;

  }
</script>

</head>
<body>
<form name="tickets" id="tickets">

<!--FIRST CLASS that takes the value entered and * by the ticket price for an adult. Price is £18 * Number of Tickets -->
	<label for="noAdults"> Number of Adults</label>
	<select name="noAdults" id="noAdults" class="adultTotal" onclick="Calc('adultTotal')">

		<option value="0" >0</option>
		<option value="1" >1</option>
		<option value="2" >2</option>
		<option value="3" >3</option>
		<option value="4" >4</option>
		<option value="5" >5</option>
		<option value="6" >6</option>
		<option value="7" >7</option>
		<option value="8" >8</option>
	</select>
	<label for="pricing"> X £18</label><br><br>
	
<!--Not class as children under 2 are not charged-->

	<label for="childUnder2"> Number of Children aged 2 or less </label>
	<select name="childUnder2" id="childUnder2">

		<option value="0" >0</option>
		<option value="1" >1</option>
		<option value="2" >2</option>
		<option value="3" >3</option>
		<option value="4" >4</option>
		<option value="5" >5</option>
		<option value="6" >6</option>
		<option value="7" >7</option>
		<option value="8" >8</option>
	</select>https://stackoverflow.com/questions/ask?title=auto%20calculate%20values%20onclick#
	<label for="pricing"> X £FREE</label><br><br>
	
<!--SECOND CLASS that takes the value entered and * by the ticket price for a child ages 3-10. Price is £10 * Number of Tickets -->	
	<label for="childBox1"> Number of Children aged 3-10</label>
	<select name="childBox1" id="childBox1" class="childBox1" onclick="Calc('childBox1')">

		<option value="0" >0</option>
		<option value="1" >1</option>
		<option value="2" >2</option>
		<option value="3" >3</option>
		<option value="4" >4</option>
		<option value="5" >5</option>
		<option value="6" >6</option>
		<option value="7" >7</option>
		<option value="8" >8</option>
	</select>
	<label for="pricing"> X £10</label><br><br>
	
<!--EDIT CLASS that takes the value entered and * by the ticket price for a child ages 11-16. Price is £13 * Number of Tickets -->
	<label for="childBox2"> Number of Children aged 11-16</label>
	<select name="childBox2" id="childBox2" class="childBox2" onclick="Calc('childBox2')">

		<option value="0" >0</option>
		<option value="1" >1</option>
		<option value="2" >2</option>
		<option value="3" >3</option>
		<option value="4" >4</option>
		<option value="5" >5</option>
		<option value="6" >6</option>
		<option value="7" >7</option>
		<option value="8" >8</option>
	</select>
	<label for="pricing"> X £13</label><br><br>

<!--If checked, the priced is double from that of a single ticket. Total price is * 2. -->
	<input type="checkbox" name="rtnJourney" id="rtnJourney" value="2" onclick="Calc('rtnJourney')">Return Journey - If unchecked, price is halved<br>
	
Total: <input type="text" id="totalCost" name="totalCost">

</form>
</body></html>

现在我明白了为什么上面的 JS 代码不起作用,我从一个在线示例中获取了这个,该示例从一个 CLASS 中获取每个值并将它们组合起来,但是由于我对每个条目都有不同的乘法值,我试图修改它,但没有锻炼。我不知道如何做我需要做的事情来计算总数。另请注意,它还没有添加值,现在我只想让它计算所有字段的总数。

ps我真的不熟悉JS,我真的很感谢提供的任何帮助。

赞光

<html>

  <head>
    <meta charset="utf-8">
    <title>Calculator</title>

    <script language="javascript">
      var map = {
        "noAdults": {
          value: 0,
          multiplier: 18
        },
        "childUnder2": {
          value: 0,
          multiplier: 0
        },
        "childBox1": {
          value: 0,
          multiplier: 10
        },
        "childBox2": {
          value: 0,
          multiplier: 13
        }
      }
      var rtnJourney = false;

      function displayTotal() {
        var total = 0;
        for (var key in map) {
          total += map[key].value * map[key].multiplier;
        }
        total = rtnJourney ? total * 2 : total;
        document.tickets.totalCost.value = '£' + total;
      }

      function getTotal(dom) {
        map[dom.id].value = dom.value;
        displayTotal();
      }

      function getChecked(dom) {
        rtnJourney = dom.checked;
        displayTotal();

      }

    </script>

  </head>

  <body>
    <form name="tickets" id="tickets">

      <!--FIRST CLASS that takes the value entered and * by the ticket price for an adult. Price is £18 * Number of Tickets -->
      <label for="noAdults"> Number of Adults</label>
      <select name="noAdults" id="noAdults" class="adultTotal" onchange="getTotal(this)">

        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
      </select>
      <label for="pricing"> X £18</label>
      <br>
      <br>

      <!--Not class as children under 2 are not charged-->

      <label for="childUnder2"> Number of Children aged 2 or less </label>
      <select name="childUnder2" id="childUnder2" onchange="getTotal(this)">

        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
      </select>https://stackoverflow.com/questions/ask?title=auto%20calculate%20values%20onclick#
      <label for="pricing"> X £FREE</label>
      <br>
      <br>

      <!--SECOND CLASS that takes the value entered and * by the ticket price for a child ages 3-10. Price is £10 * Number of Tickets -->
      <label for="childBox1"> Number of Children aged 3-10</label>
      <select name="childBox1" id="childBox1" class="childBox1" onchange="getTotal(this)">

        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
      </select>
      <label for="pricing"> X £10</label>
      <br>
      <br>

      <!--EDIT CLASS that takes the value entered and * by the ticket price for a child ages 11-16. Price is £13 * Number of Tickets -->
      <label for="childBox2"> Number of Children aged 11-16</label>
      <select name="childBox2" id="childBox2" class="childBox2" onchange="getTotal(this)">

        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
      </select>
      <label for="pricing"> X £13</label>
      <br>
      <br>

      <!--If checked, the priced is double from that of a single ticket. Total price is * 2. -->
      <input type="checkbox" name="rtnJourney" id="rtnJourney" value="2" onchange="getChecked(this)">Return Journey - If unchecked, price is halved
      <br> Total:
      <input type="text" id="totalCost" name="totalCost">

    </form>
  </body>

</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章