【问题标题】:multiply selectors with input values in jquery将选择器与jquery中的输入值相乘
【发布时间】:2015-05-29 10:39:44
【问题描述】:

我的计算不太好。也许你可以帮助我:

  <input id="count" min="1" value="1" class ="txtMult form-control" type="number" name="txtEmmail" />
  <input type="text" value="250"  class ="txtMult" name="txtEmmail"/>
  <span class="multTotal">250</span>

<select class="selectpicker">
<option value="1">multiply with 1</option>
<option value="2">multiply with 2</option>
</select>

<span id="grandTotal">250</span>

这里是 Javascript:

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


$(window).load(function(){
$('select').change(function() {
   $("#grandTotal").html($('select').val()*$('.multTotal').html())
});
});
</script>

所以我希望#grandTotal 总是正确地改变。如果选择例如“乘以 2”并且在更改我的“#count”的值后,“#grandTotal”应该会自动显示正确的数字。我对计算感到很困惑。也许你看得更清楚。非常感谢

【问题讨论】:

  • 什么是错误计算或更改事件不起作用的问题。

标签: jquery multiplication


【解决方案1】:

工作小提琴 http://jsfiddle.net/anilk/6fLk8w8a/1/

更新的js

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



$('.selectpicker').change(function() {
    calcVal();
});

function calcVal(){
    alert($('select').val()*$('.multTotal').html());
   $("#grandTotal").html($('select').val()*$('.multTotal').html())
}

// call on document load
calcVal();

您的绑定不正确,请使用正确的类。

【讨论】:

    【解决方案2】:

    在 javascript 端计算时,您必须解析为 int 或 float。

    您可以使用parseIntparseFloat()

    var i = parseInt( $('select').val()) * parseInt( $('.multTotal').html())
    $("#grandTotal").html(i);
    

    【讨论】:

      猜你喜欢
      • 2011-06-30
      • 2013-09-19
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多