我有这个脚本,它将4个数字相加,但JS没有考虑到最初的小数位。
//初始输入表单
<form class="weight_form" data-ajax="false" method="post">
<input type="text" class="input_save" placeholder="save as.."/>
<input type="text" id="lf" placeholder="enter LF lbs" value="" onkeyup="myFunction()"/>
<input type="text" id="rf" placeholder="enter RF lbs" value="" onkeyup="myFunction()"/>
<input type="text" id="lr" placeholder="enter LR lbs" value="" onkeyup="myFunction()"/>
<input type="text" id="rr" placeholder="enter RR lbs" value="" onkeyup="myFunction()"/>
<input type="submit" id="sub_save" value="save"/>
//显示结果的Section
<div id="bottom_section">
<input type="text" id="total" value=""/>
<input type="text" id="cross" value=""/>
<input type="text" id="ls" value=""/>
<input type="text" id="bite" value=""/>
</div>
// JS动态执行操作的脚本
<script>
function myFunction() {
var lf = document.getElementById("lf");
var rf = document.getElementById("rf");
var lr = document.getElementById("lr");
var rr = document.getElementById("rr");
var lf = parseInt(lf.value);
var rf = parseInt(rf.value);
var lr = parseInt(lr.value);
var rr = parseInt(rr.value);
var lf_1 = lf*1;
var rf_1 = rf*1;
var lr_1 = lr*1;
var rr_1 = rr*1;
var total = lf_1 + rf_1 + lr_1 + rr_1;
var cross = rf_1 + lr_1 / total;
var ls = lf_1 + lr_1 / total;
var bite = lr_1 - rr_1;
$('#total').val(total);
$('#cross').val(Math.round(cross));
$('#ls').val(Math.round(ls));
$('#bite').val(bite);
};
</script>
如果我为"weight_form“(lf,rf,lr,rr)中的每一个输入输入比方说20.5,那么总数就是80而不是82。为什么它似乎总是将值向下舍入?
https://stackoverflow.com/questions/51661695
复制相似问题