Javascript-如何让此脚本显示计算的总数?

时间:2017-05-21 01:17:21

标签: javascript html

我知道这个脚本中的其他错误,但我知道如何解决这些问题,主要问题是我无法弄清楚如何修复此脚本底部的计算总函数。我是相当新的,但在检查了几个指南后,我找不到任何有用的东西。

 <html>
      <head>
      </head>
      <body>
        <table>
          <tr>
            <td class="ColHdr">ID</td>
            <td class="ColHdr">Product</td>
            <td class="ColHdr">Price</td>
            <td class="ColHdr">Discounted Price</td>
            <td class="ColHdr">Quantity</td>
          </tr>
          <tr>
            <td>1</td>
            <td class="Product">Hat</td>
            <td class="Price" id="Price01">19.99</td>
            <td class="Price" id="DiscountedPrice01"></td>
            <td><input id="Quantity01" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>2</td>
            <td class="Product">Shirt</td>
            <td class="Price" id="Price02">27.99</td>
            <td class="Price" id="DiscountedPrice02"></td>
            <td><input id="Quantity02" type="number" value="0" `enter code here`onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>3</td>
            <td class="Product">Jersey</td>
            <td class="Price" id="Price03">109.99</td>
            <td class="Price" id="DiscountedPrice03"></td>
            <td><input id="Quantity03" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>4</td>
            <td class="Product">Socks</td>
            <td class="Price" id="Price04">4.49</td>
            <td class="Price" id="DiscountedPrice04"></td>
            <td><input id="Quantity04" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>5</td>
            <td class="Product">Sweatshirt</td>
            <td class="Price" id="Price05">39.79</td>
            <td class="Price" id="DiscountedPrice05"></td>
            <td><input id="Quantity05" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>6</td>
            <td class="Product">Bumper Sticker</td>
            <td class="Price" id="Price06">3.29</td>
            <td class="Price" id="DiscountedPrice06"></td>
            <td><input id="Quantity06" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>7</td>
            <td class="Product">Jacket</td>
            <td class="Price" id="Price07">179.99</td>
            <td class="Price" id="DiscountedPrice07"></td>
            <td><input id="Quantity07" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>8</td>
            <td class="Product">Sun Glasses</td>
            <td class="Price" id="Price08">12.99</td>
            <td class="Price" id="DiscountedPrice08"></td>
            <td><input id="Quantity08" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>9</td>
            <td class="Product">Poster</td>
            <td class="Price" id="Price09">8.99</td>
            <td class="Price" id="DiscountedPrice09"></td>
            <td><input id="Quantity09" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>10</td>
            <td class="Product">Banner</td>
            <td class="Price" id="Price10">5.49</td>
            <td class="Price" id="DiscountedPrice10"></td>
            <td><input id="Quantity10" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>11</td>
            <td class="Product">Ski Hat</td>
            <td class="Price" id="Price11">15.09</td>
            <td class="Price" id="DiscountedPrice11"></td>
            <td><input id="Quantity11" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>12</td>
            <td class="Product">Tee Shirt</td>
            <td class="Price" id="Price12">14.99</td>
            <td class="Price" id="DiscountedPrice12"></td>
            <td><input id="Quantity12" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
            </tr>
          <tr>
            <td>13</td>
            <td class="Product">Tank Top</td>
            <td class="Price" id="Price13">9.99</td>
            <td class="Price" id="DiscountedPrice13"></td>
            <td><input id="Quantity13" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>14</td>
            <td class="Product">Pin</td>
            <td class="Price" id="Price14">1.79</td>
            <td class="Price" id="DiscountedPrice14"></td>
            <td><input id="Quantity14" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>15</td>
            <td class="Product">Framed Photo</td>
            <td class="Price" id="Price15">49.99</td>
            <td class="Price" id="DiscountedPrice15"></td>
            <td><input id="Quantity15" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>16</td>
            <td class="Product">Team License Plate</td>
            <td class="Price" id="Price16">17.99</td>
            <td class="Price" id="DiscountedPrice16"></td>
            <td><input id="Quantity16" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>17</td>
            <td class="Product">Team Pen</td>
            <td class="Price" id="Price17">3.99</td>
            <td class="Price" id="DiscountedPrice17"></td>
            <td><input id="Quantity17" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>18</td>
            <td class="Product">Decorative Baseball</td>
            <td class="Price" id="Price18">11.99</td>
            <td class="Price" id="DiscountedPrice18"></td>
            <td><input id="Quantity18" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>19</td>
            <td class="Product">Decorative Football</td>
            <td class="Price" id="Price19">29.99</td>
            <td class="Price" id="DiscountedPrice19"></td>
            <td><input id="Quantity19" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>20</td>
            <td class="Product">Decorative Basketball</td>
            <td class="Price" id="Price20">24.99</td>
            <td class="Price" id="DiscountedPrice20"></td>
            <td><input id="Quantity20" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>21</td>
            <td class="Product">Window Decal</td>
            <td class="Price" id="Price21">5.99</td>
            <td class="Price" id="DiscountedPrice21"></td>
            <td><input id="Quantity21" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td>22</td>
            <td class="Product">Window Decal (Team Set)</td>
            <td class="Price" id="Price22">19.99</td>
            <td class="Price" id="DiscountedPrice22"></td>
            <td><input id="Quantity22" type="number" value="0" onchange="CheckQuantity(this.value, this.id);"></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td><br><input type="button" id="CalculateTotal" value="Calculate Total" onclick="CalculcateTotal();"></td>
            <td></td>
            <td id="TotalLabel"><br>Total:</td>
            <td id="Total"><br></td>
          </tr>
        </table>
        <br>
        <p>
          Plan:
          &nbsp;&nbsp;&nbsp;
          <input type="radio" value="1" name="Plan" checked="checked" onclick="CalcDiscountedPrice(this.value, this.id);">Standard
          &nbsp;&nbsp;&nbsp;
          <input type="radio" value=".95" name="Plan" onclick="CalcDiscountedPrice(this.value, this.id);">Silver
          &nbsp;&nbsp;&nbsp;
          <input type="radio" value=".85" name="Plan" onclick="CalcDiscountedPrice(this.value, this.id);">Gold
        </p>
        <img src="AcmeLogo.jpg" onload="CalcDiscountedPrice(1);">
      <body>
      <script type="text/javascript" >
        function CalcDiscountedPrice(PercentToPay) {
          var i = 1;
          for (i = 1; i <= 22; i++) {
            if (i < 10) {
              var CellNumber = "0" + i;
            } else {
              var CellNumber = i;
            }
            var DiscountedCellId = "DiscountedPrice" + CellNumber;
            var DiscountedCell = document.getElementById(DiscountedCellId);
            var PriceCellId = "Price" + CellNumber;
            var PriceCell = document.getElementById(PriceCellId);
            var Price = Number(PriceCell.innerHTML);
            var DiscountedPrice = Math.round((Price * PercentToPay) * 100)/100;
            DiscountedCell.innerHTML = DiscountedPrice;
            DiscountedCell.style.color = "#ee0c00";
            DiscountedCell.style.fontWeight = "bold";
            DiscountedCell.style.fontStyle = "italic";
          }
        }

        function CalculateTotal() {
          var total= O;
           var i = 1;
          for (var i = 1; b <= 22; i++) {
            if (i < 10) {
              var CellNumber = "0" + i;
            } else {
              var CellNumber = i;
            }
         var DiscountedCellId = "DiscountedPrice" + CellNumber;
            var DiscountedCell = document.getElementById(DiscountedCellId);
        }

        }
      </script>
    </html>

2 个答案:

答案 0 :(得分:0)

你有一个错字,你正在打电话

 onclick="CalculcateTotal();"

但你的功能是

function CalculateTotal() {

将您的onclick更改为

onclick="CalculateTotal();"

尝试F-12。该浏览器中有一个调试器。

答案 1 :(得分:0)

下面修改的是你的javascript。请注意,我没有将所有变量都资本化,因为这是不好的做法。

function CalcDiscountedPrice(percentToPay) {
    var i = 1;

    for (i = 1; i <= 22; i++) {

        if (i < 10) {
            var cellNumber = "0" + i;
        } else {
            var cellNumber = i;
        }

        var discountedCellId = "DiscountedPrice" + cellNumber;
        var discountedCell = document.getElementById(discountedCellId);
        var priceCellId = "Price" + cellNumber;
        var priceCell = document.getElementById(priceCellId);
        var price = Number(priceCell.innerHTML);
        var discountedPrice = Math.round((price * percentToPay) * 100)/100;
        discountedCell.innerHTML = discountedPrice;
        discountedCell.style.color = "#ee0c00";
        discountedCell.style.fontWeight = "bold";
        discountedCell.style.fontStyle = "italic";
    }

    CalculateTotal();
}

function CalculateTotal() {
    var total= 0;
    var i = 1;
    for (var i = 1; i <= 22; i++) {
        if (i < 10) {
          var cellNumber = "0" + i;
        } else {
          var cellNumber = i;
        }

        var discountedCellId = "DiscountedPrice" + cellNumber;
        var discountedCellPrice = document.getElementById(discountedCellId).innerHTML;

        var price = discountedCellPrice;
        if(discountedCellPrice === "") { // check if a discount is applied, use normal price if not
            var normalCellId = "Price" + cellNumber;
            price = document.getElementById(normalCellId).innerHTML;
        }

        var quantityCellId = "Quantity" + cellNumber;
        total += price * document.getElementById(quantityCellId).value;
    }
    document.getElementById("Total").innerHTML = total;
}

正如@RamblinRose指出的那样,你还没有正确调用CalculateTotal