将输入字段与type =“number”相乘

时间:2015-05-29 08:22:51

标签: jquery multiplying

我想将我的输入字段与此示例相乘:http://jsfiddle.net/nnnnnn/5FpWC/

 $(document).ready(function () {
   $(".txtMult input").keyup(multInputs);

   function multInputs() {
       var mult = 0;
       // for each row:
       $("tr.txtMult").each(function () {
           // get the values from this row:
           var $val1 = $('.val1', this).val();
           var $val2 = $('.val2', this).val();
           var $total = ($val1 * 1) * ($val2 * 1)
           $('.multTotal',this).text($total);
           mult += $total;
       });
       $("#grandTotal").text(mult);
   }  });

<tr class="txtMult">
        <td>
            <input name="txtEmmail" class="val1"  />
        </td>
        <td>
            <input name="txtEmmail" class="val2"/>
        </td>
        <td>
                <span class="multTotal">0.00</span>
        </td>
    </tr>

我现在的问题是我需要一个输入字段作为type =“number”,默认值为“1”。但是当我将类型更改为数字时,计算不再有效。你知道原因吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

我找到了解决方案!

$(function(){
            $('.in input').change(function(){
                var p=$(this).parent().parent()
                var m=p.find('input.txtMult')
                var mul=parseFloat($(m[0]).val()*$(m[1]).val()).toFixed(2)
                var res=p.find('.multTotal')
                res.html(mul);
                var total=0;
                $('.in .multTotal').each(function(){
                    total+=parseFloat($(this).html())
                })
                $('.in #grandTotal').html(parseFloat(total).toFixed(2))
            });
        })

table class='in'>
        <tr>
            <td>
                <input value="0" class ="txtMult" type="number" name="txtEmmail" />
            </td>
            <td>
                <input value="0"  class ="txtMult" name="txtEmmail"/>
            </td>
            <td>
                <span class="multTotal">0.00</span>
            </td>
        </tr>
        <tr>
            <td>
                <input value="0"  class ="txtMult" name="txtEmmail"/>
            </td>
            <td>
                <input value="0"  class ="txtMult" name="txtEmmail"/>
            </td>
            <td>
                <span class="multTotal">0.00</span>
            </td>
        </tr>
        <tr>
            <td colspan="3" align="right">
                Grand Total# <span id="grandTotal">0.00</span>
            </td>
        </tr>
    </table>