如何使用javascript将两个文本字段相乘并将结果输出到另一个文本字段中

时间:2012-11-08 23:08:07

标签: javascript jquery ajax

我正在使用此代码

   <script type="text/javascript">
    $(function() {
        $("#addAll2").click(function() {
            var add = 0;
            $("#discount") = $dis
            $(".amt2").each(function() {
                add += document.getElementById('subtotal').value * document.getElementById('discount').value / 100;
            });
            $("#discountamt").val(add.toFixed(2));
        });
    });
</script>
<script type="text/javascript">
    $(function() {
        $("#addAll").click(function() {
            var add = 0;
            $(".amt").each(function() {
                add += Number($(this).val());
            });
            $("#subtotal").val(add.toFixed(2));
        });
    });
</script>
<script type="text/javascript">
    $(function() {
        $("#addAll3").click(function() {
            var add = 0;
            $(".amt3").each(function() {
                add += Number($(this).val());
            });
            $("#sum").val(add.toFixed(2));
        });
    });
</script>

与这些字段一起:

    <fieldset>
  <legend>Items</legend>
  <table>
    <thead>
      <tr>
        <th scope="col" width="100px">ALU</th>
        <th scope="col" width="400px">Item Name</th>
        <th scope="col" width="100px">Item Price</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><input name="alu1" id="alu1" type="text" size="15" /></td>
        <td><input name="itemname1" id="itemname1" type="text" size="75" /></td>
        <td>$
          <input name="price1" id="price1" class="currency amt amt1" type="text" size="14" value="0.00" /></td>
      </tr>
    </tbody>
  </table>
  <button>Add Row</button>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
  <script>
    $(document).ready(function($)
    {
        // trigger event when button is clicked
        $("button").click(function()
        {
            // add new row to table using addTableRow function
            addTableRow($("table"));
            // prevent button redirecting to new page
            return false;
        });

        // function to add a new row to a table by cloning the last row and 
        // incrementing the name and id values by 1 to make them unique
        function addTableRow(table)
        {
            // clone the last row in the table
            var $tr = $(table).find("tbody tr:last").clone();
            // get the name attribute for the input and select fields
            $tr.find("input,select").attr("name", function()
            {
                // break the field name and it's number into two parts
                var parts = this.id.match(/(\D+)(\d+)$/);
                // create a unique name for the new field by incrementing
                // the number for the previous field by 1
                return parts[1] + ++parts[2];
            // repeat for id attributes
            }).attr("id", function(){
                var parts = this.id.match(/(\D+)(\d+)$/);
                return parts[1] + ++parts[2];
            });
            // append the new row to the table
            $(table).find("tbody tr:last").after($tr);
        };
    });
    </script>
 <div class="fieldline" style="text-align:right;">
   <input id="addAll" type="button" value="Calculate Subtotal" />
    <label>Subtotal: $
    <input size="18" name="subtotal" id="subtotal" class="amt2 amt3" type="text" value="0.00" />
    </label>
    </div>
    <div class="fieldline" style="text-align:right;">
    <label>Discount:
    <input size="18" name="discount" id="discount" type="text" value="00" />%
    </label>
    </div>
    <div class="fieldline" style="text-align:right;">
    <input id="addAll2" type="button" value="Calculate Discount" />
    <label>Discount Amount: $
    <input size="18" name="discountamt" id="discountamt" class="amt3" type="text" value="0.00" />
    </label>
    </div>
  <div class="fieldline" style="text-align:right;">
    <label>Shipping Method:
      <select name="shipmeth" id="shipmeth" onchange="MM_jumpMenu('parent',this,0)">
        <option></option>
        <option value="freeups">FREE UPS Ground</option>
        <option value="upsground">UPS Ground</option>
        <option value="ups2">UPS 2nd Day</option>
        <option value="ups3">UPS 3rd Day</option>
        <option value="ups1">UPS Next Day Air</option>
        <option value="upscanada">UPS Canada</option>
        <option value="uspsint">USPS International</option>
        <option value="dhlint">DHL International</option>
        <option value="freeship">Free Shipping Override</option>
      </select>
    </label>
  </div>
  <div class="fieldline" style="text-align:right;">
    <label>Shipping Cost: $
      <input size="18" name="shipcost" id="shipcost" class="amt3" type="text" value="0.00" />
    </label>
  </div>
  <div class="fieldline" style="text-align:right;">
    <input id="addAll1" type="button" value="Calculate CA TAX" />
    <label>Sales Tax: $
      <input name="salestax" type="text" id="salestax" class="amt3" size="18" value="0.00" />
    </label>
  </div>
  <div class="fieldline" style="text-align:right;">
    <input id="addAll" type="button" value="Calculate Total" />
    <label>Total:
      <input size="18" required="required" name="sum" id="sum" type="text" value="0.00" />
    </label>
  </div>
</fieldset>

我有它所以它计算小计并将其放入。但是当我尝试允许它取得折扣字段并乘以它然后除以100输出结果在折扣Ammount字段中。它似乎不起作用。我也尝试了不同的变化。

如果可以获得折扣,我应该能够将其余部分计算在总计字段中。

感谢!!!!

1 个答案:

答案 0 :(得分:1)

该行

$("#discount") = $dis

看起来不对我 - 尝试删除它。我在这个JSFiddle中对它进行了评论,它似乎做了你想做的事。