从数字输入计算总数并设置为数字输入 [英] Calculate total from number input and set to number input

查看:79
本文介绍了从数字输入计算总数并设置为数字输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我的计算脚本有一些问题,为什么我不能计算示例:23 + 20 * 2 = 86当我计算他收集+而不是*为什么?这是脚本

Helo, I have some problems on my calculate script, why I can not calculate example: 23+20*2=86 when I calculate he gather + and not * why ? hre is the script

<script type='text/javascript'>
$(window).load(function(){
    $('div#cont-sum-fields').on('change', 'input', function() {
        var total = 0;
        $('#cont-sum-fields').find('input[id^="cont-sum"]').each(function() {
            if ($(this).val() !== "") total += parseFloat($(this).val())
        });
        $('#cont-sum-fields').find('#total-cont-sum').val(total);
    });
});//]]>  
</script>


<body>
  <div id='cont-sum-fields'>
    <input type="number" id="cont-sum-1" /> 
    <input type="number" id="cont-sum-2" /> 
    <input type="number" id="cont-sum-3" /> 
    <input id="total-cont-sum" type="number" disabled />
  </div>
</body>

我在哪里错了?

推荐答案

这是您想要的吗?

$(window).load(function(){
    $('div#cont-sum-fields').on('change', 'input', function() {
        var total =  (parseInt($( "#cont-sum-1" ).val()) +  parseInt($( "#cont-sum-2" ).val())) * parseInt($( "#cont-sum-3" ).val()) ;

        $('#cont-sum-fields').find('#total-cont-sum').val(total);
    });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  <div id='cont-sum-fields'>
    (<input type="number" id="cont-sum-1" /> +
    <input type="number" id="cont-sum-2" />) x
    <input type="number" id="cont-sum-3" /> 
    <input id="total-cont-sum" type="number" disabled />
  </div>

这篇关于从数字输入计算总数并设置为数字输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆