我创建了一个多输入汇率,我想计算多个输入文本字段*选择选项设计的总和 我找到了解决方案但问题是当我把0读起来像1:
$('input.summable,.multiply').keyup(function() {
var rate = 0;
var box = 0;
rate += $('input.summable').val();
box += $('.multiply').val();
var sum = (rate *box);
$('input.summable,.multiply').each(function() { sum += parseFloat(this.value); });
$('#totalsum').val(sum);
});
答案 0 :(得分:0)
.summable
与.multiply
相乘,则不需要each
循环。select
没有keyup
事件,您需要change
事件。
// add change event too
$('.summable, .multiply').on("keyup change", function() {
var rate = parseFloat($('.summable').val());
var box = parseFloat($('.multiply').val());
var sum = rate * box;
// remove this
// $('.summable, .multiply').each(function() {
// sum += parseFloat(this.value);
// });
$('#totalsum').val(sum);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
sum:
<input class="summable" type="text">
<br />
multi:
<select class="multiply">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<br />
total:
<input id="totalsum" type="text">
&#13;
答案 1 :(得分:0)
我的建议基于:
我的例子:
$('input.summable,.multiply').on('input change', function (e) {
var sum = 0;
$('input.summable').each(function(i, e) {
sum += (+this.value * +$('.multiply').eq(i).val());
});
$('#totalsum').val(sum);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
Montant: <input class="summable form-text" type="text" name="name" value size="60" maxlength="128"><br>
Device: <select class="multiply form-select" name="money">
<option value="5.91">BHD</option>
<option value="1.712">CAD</option>
<option value="33.245">DKK</option>
</select>
<br>
Montant: <input class="summable form-text" type="text" id="edit-name" name="name" value size="60" maxlength="128"><br>
Device: <select class="multiply form-select" name="money">
<option value="5.91">BHD</option>
<option value="1.712">CAD</option>
<option value="33.245">DKK</option>
</select>
<br>
En Dinnar: <input id="totalsum" type="text">
</form>