Helo,我的计算脚本有一些问题,为什么我无法计算例子:23 + 20 * 2 = 86当我计算他聚集+而不是*为什么?这是剧本
<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>
哪里错了?
答案 0 :(得分:3)
这是你想要的吗?
$(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);
});
});
&#13;
<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>
&#13;