我有两个html.textBoxFor
并在两个值之间进行减法,但结果仍显示为.
,而不是,
。我该如何解决这个问题?
<script type="text/javascript">
$(document).ready(function () {
$("#km_final").on('input', function () {
sumCal();
});
$("#km_inicial").on('input', function () {
sumCal();
});
function sumCal() {
var km1 = parseFloat($("#km_inicial").val().replace(",","."));
var km2 = parseFloat($("#km_final").val().replace(",", "."));
var result = (km2) - (km1);
if (!isNaN(result)) {
$("#extensao").val(result);
} else {
$("#extensao").val(0);
}
}
});
</script>
答案 0 :(得分:0)
您通过将,
翻译为.
来正确地将输入转换为真实数字,但在计算出正确的浮点数后,您并未将.
翻译回来进入,
s: Working solution :
$(document).ready(function () {
$("#km_final").on('input', function () {
sumCal();
});
$("#km_inicial").on('input', function () {
sumCal();
});
function sumCal() {
var km1 = parseFloat($("#km_inicial").val().replace(",", "."));
var km2 = parseFloat($("#km_final").val().replace(",", "."));
var result = km2 - km1;
if (!isNaN(result)) {
$("#extensao").val(result.toString().replace(".", ","));
} else {
$("#extensao").val(0);
}
}
});