我在下面有这段 JavaScript 代码,用于获取三个文本字段的总和值。显示结果时我需要显示两位小数。请帮我。我尝试了很多方法来获得小数位,但找不到方法。
<script type="text/javascript">
function num(id) {
var e = document.getElementById(id);
if (e != null) {
var v = e.value;
if (/^\d+$/.test(v)) {
return parseInt(v, 10);
}
}
return 0;
}
function sum() {
var v1 = num("contNo6_1");
var v2 = num("contNo6_2");
var v3 = num("contNo6_3");
var r = document.getElementById("result");
r.toFixed(2);
if (r != null) {
r.value = v1 + v2 + v3;
}
}
function addHandler(element, eventName, handler) {
if (element.addEventListener) {
element.addEventListener(eventName, handler, false);
} else if (element.attachEvent) {
element.attachEvent("on" + eventName, handler);
}
}
addHandler(window, "load",
function() {
addHandler(document.getElementById("contNo6_1"), "keyup", sum);
addHandler(document.getElementById("contNo6_2"), "keyup", sum);
addHandler(document.getElementById("contNo6_3"), "keyup", sum);
});
</script>