【问题标题】:Create a "time savings" calculator with dynamic figures使用动态数字创建“节省时间”计算器
【发布时间】:2011-09-13 09:05:50
【问题描述】:

我的任务是在网页上创建一个动态计算器。

它将包括以下内容:

  1. 将通过后端 (Wordpress) 输入的几个数字。
  2. 客户将输入数字的几个输入区域。
  3. 还有几个数字会根据客户输入的内容而变化。

会有几个计算改变页面上的数字。

我可以轻松地从 WP 输出数字,这只是我正在努力解决的第 2 点和第 3 点。解决这个问题的最佳方法是什么?任何人都可以发布一些包含上述 3 点的简单示例吗?

一个重要的因素是它必须即时重新计算数字 - 所以不要点击提交按钮或类似的任何东西。

【问题讨论】:

  • 您到底需要计算什么,您的价值观是什么,您的价值观如何相互关联?这是一项相当简单的任务(很明显,这是一项 javascript 工作),但在不了解您拥有哪些数字、用户将输入哪些数字、应该显示什么结果以及应该以什么格式显示的情况下,它有点难以进一步指导您...
  • @DaveRandom 感谢您的回复。目前我只是在寻找例子,所以一些基本的计算和一些动态变化的基本输入会很好,因为我可以更进一步。我只是不知道从哪里开始。

标签: php javascript wordpress calculator


【解决方案1】:

第 1 点

<script>
var unitPrice = <?php echo $unitPrice; ?>;
...
</script>

第 2 点

<input type="text" id="quantity" onkeyup="updateTotal();/>

第 3 点

<span id="totalPrice"></span>

<script>
function updateTotal() {
    var quantity   = parseInt(document.getElementById('quantity').value, 10);
    if(isNaN(quantity)) quantity = 0;
    var totalPrice = unitPrice * quantity;
    document.getElementById('totalPrice').innerText = totalPrice;
}
</script>

演示here.

【讨论】:

    【解决方案2】:

    这是一个简单的计算器,我刚刚把它放在一起,它可以从任何一个属性中计算出圆的其他属性。它利用onkeyup 事件,因此值会在您键入时更新。它绝不是完美的(甚至对数学没有 100% 的把握),但这不是重点,重点是演示如何进行计算和实时更新显示,希望它能给你一个推动力正确的方向。

    在这里详细解释太复杂了,所以请玩一下并提出具体问题,我会尽我所能回答......

    更新代码

    <html>
    
      <head>
        <title>Circle calculator</title>
        <script type="text/javascript">
    
          var radius, diameter, circumference, area, sphereVolume, lastRadius, lastDiameter, lastCircumference, lastArea, lastSphereVolume;
    
          function updateDisplays () {
            // This function updates the displayed values
            document.getElementById('radius_input').value = (isNaN(radius)) ? '0.0' : ((radius.toString().split('.').length == 1) ? radius.toString()+'.0' : radius);
            document.getElementById('diameter_input').value = (isNaN(diameter)) ? '0.0' : ((diameter.toString().split('.').length == 1) ? diameter.toString()+'.0' : diameter);
            document.getElementById('circumference_input').value = (isNaN(circumference)) ? '0.0' : ((circumference.toString().split('.').length == 1) ? circumference.toString()+'.0' : circumference);
            document.getElementById('area_input').value = (isNaN(area)) ? '0.0' : ((area.toString().split('.').length == 1) ? area.toString()+'.0' : area);
            document.getElementById('spherevolume_input').value = (isNaN(sphereVolume)) ? '0.0' : ((sphereVolume.toString().split('.').length == 1) ? sphereVolume.toString()+'.0' : sphereVolume);
            // Track the last values
            lastRadius = (isNaN(radius)) ? 0 : radius;
            lastDiameter = (isNaN(diameter)) ? 0 : diameter;
            lastCircumference = (isNaN(circumference)) ? 0 : circumference;
            lastArea = (isNaN(area)) ? 0 : area;
            lastSphereVolume = (isNaN(sphereVolume)) ? 0 : sphereVolume;
          }
    
          window.onload = function () {
    
            // All these functions calculate the other values based on the one that has changed
    
            document.getElementById('radius_input').onkeyup = function () {
              radius = parseFloat(this.value);
              if (lastRadius == radius) {
                return;
              }
              diameter = radius * 2;
              circumference = Math.PI * diameter;
              area = Math.PI * Math.pow(radius, 2);
              sphereVolume = (4 / 3) * Math.PI * Math.pow(radius, 3);
              updateDisplays();
            };
    
            document.getElementById('diameter_input').onkeyup = function () {
              diameter = parseFloat(this.value);
              if (lastDiameter == diameter) {
                return;
              }
              radius = diameter / 2;
              circumference = Math.PI * diameter;
              area = Math.PI * Math.pow(radius, 2);
              sphereVolume = (4 / 3) * Math.PI * Math.pow(radius, 3);
              updateDisplays();
            };
    
            document.getElementById('circumference_input').onkeyup = function () {
              circumference = parseFloat(this.value);
              if (lastCircumference == circumference) {
                return;
              }
              diameter = circumference / Math.PI;
              radius = diameter / 2;
              area = Math.PI * Math.pow(radius, 2);
              sphereVolume = (4 / 3) * Math.PI * Math.pow(radius, 3);
              updateDisplays();
            };
    
            document.getElementById('area_input').onkeyup = function () {
              area = parseFloat(this.value);
              if (lastArea == area) {
                return;
              }
              radius = Math.sqrt(area / Math.PI);
              diameter = radius * 2;
              circumference = Math.PI * diameter;
              sphereVolume = (4 / 3) * Math.PI * Math.pow(radius, 3);
              updateDisplays();
            };
    
            document.getElementById('spherevolume_input').onkeyup = function () {
              sphereVolume = parseFloat(this.value);
              if (lastSphereVolume == sphereVolume) {
                return;
              }
              radius = Math.pow((sphereVolume / Math.PI) * (3/4), 1/3);
              diameter = radius * 2;
              area = Math.PI * Math.pow(radius, 2);
              circumference = Math.PI * diameter;
              updateDisplays();
            };
    
          };
        </script>
        <style>
          #title {
            margin-bottom: 20px;
            font-weight: bold;
          }
          td.property_name {
            text-align: right;
            padding-right: 10px;
          }
          td.property_formula {
            padding-left: 10px;
            font-weight: bold;
          }
        </style>
      </head>
    
      <body>
        <div id="title">Circle Calculator</div>
        <table>
          <tr>
            <td class="property_name">Radius:</td>
            <td><input type="text" id="radius_input" value="0.0" /> cm</td>
            <td class="property_formula"></td>
          </tr>
          <tr>
            <td class="property_name">Diameter:</td>
            <td><input type="text" id="diameter_input" value="0.0" /> cm</td>
            <td class="property_formula">2r</td>
          </tr>
          <tr>
            <td class="property_name">Circumference:</td>
            <td><input type="text" id="circumference_input" value="0.0" /> cm</td>
            <td class="property_formula">2&#960;r</td>
          </tr>
          <tr>
            <td class="property_name">Area:</td>
            <td><input type="text" id="area_input" value="0.0" /> cm<sup>2</sup></td>
            <td class="property_formula">&#960;r<sup>2</sup></td>
          </tr>
          <tr>
            <td class="property_name">Volume of Sphere:</td>
            <td><input type="text" id="spherevolume_input" value="0.0" /> cm<sup>3</sup></td>
            <td class="property_formula"><sup>4</sup>&frasl;<sub>3</sub>&#960;r<sup>3</sup></td>
          </tr>
        </table>
      </body>
    
    </html>
    

    编辑

    一个更简单的例子,只做 1 次加法:

    <html>
    
      <head>
        <title>Simple Calculator</title>
        <script type="text/javascript">
    
          window.onload = function () {
          // All code here is executed once the document has finished loading
    
            // Assign a function to num1's onkeyup event
            document.getElementById('num1').onkeyup = function () {
              // Declare the variables
              var num1, num2;
              // Get the values in variables with the correct type for calculations
              num1 = parseFloat(this.value);
              num2 = parseFloat(document.getElementById('num2').value);
              // Update the answer display
              document.getElementById('answer').innerHTML = num1 + num2;
            };
    
            // Assign a function to num2's onkeyup event
            document.getElementById('num2').onkeyup = function () {
              // Declare the variables
              var num1, num2;
              // Get the values in variables with the correct type for calculations
              num1 = parseFloat(document.getElementById('num1').value);
              num2 = parseFloat(this.value);
              // Update the answer display
              document.getElementById('answer').innerHTML = num1 + num2;
            };
    
          };
    
        </script>
      </head>
    
      <body>
        <input type="text" id="num1" size="4" value="0" /> + <input type="text" id="num2" size="4" value="0" /> = <span id="answer">0</span>
      </body>
    
    </html>
    

    【讨论】:

    • 感谢您的示例。我会看看我能从中得到什么。正在寻找更简单的东西,比如 a+b=c 有点简单! a=一个wordpress字段,b=一个输入,c=答案。所有这些都是即时完成的,因此无需单击提交按钮或重新加载页面。
    猜你喜欢
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多