【发布时间】:2019-05-21 15:33:02
【问题描述】:
- 我正在尝试做加法——将所有价格相加和减法——减去价格函数
- 结果为十进制/浮点数
$('.add').keyup(function () {
var sum = 0;
$('.add').each(function() {
sum += Number($(this).val());
});
$('#total').val(sum);
});
<strong>price1 : </strong>
<input type="text" name="IT" class="add" style="width:150px" required><br><br>
<strong>price2 : </strong>
<input type="text" name="roadtax" class="add" style="width:150px" required><br><br>
<strong>price3 : </strong>
<input type="text" name="servis" class="add" style="width:150px" required><br><br>
<strong>price4 : </strong>
<input type="text" name="other" class="add" style="width:150px" required><br><br>
<strong>discount : </strong>
<input type="text" name="disc" class="sub" style="width:150px" required><br><br>
<strong>total : </strong>
<input type="text" id="total" name="total" style="width:150px">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>
- 可以做减法
- 我希望输出为十进制/浮点数
【问题讨论】:
-
“我希望输出为小数/浮点数” - 如果您输入小数/浮点数。如果你只输入整数,你会得到一个整数。
标签: javascript html add subtraction