我发现这个文件计算了两个文本框值的总和,问题是我需要用逗号来表示,如1000>我的总文本框中有1,000个。
以下是代码。
<!doctype html>
<html>
<head>
<script>
var x = 0;
var y = 0;
var z = 0;
function calc(obj) {
var e = obj.id.toString();
if (e == 'tb1') {
x = Number(obj.value);
y = Number(document.getElementById('tb2').value);
} else {
x = Number(document.getElementById('tb1').value);
y = Number(obj.value);
}
z = x + y;
document.getElementById('total').value = z;
}
</script>
</head>
<form name="addem" action="" id="addem" >
<p><input type="text" id="tb1" name="tb1" onkeyup="calc(this)"/>1</p>
<p><input type="text" id="tb2" name="tb2" onkeyup="calc(this)"/>2</p>
<input type="text" id="total" name="total" value="0" />
</form>
</body>
</html>
答案 0 :(得分:0)
您可以使用任何库格式化Numeral.js
等数字,您的修改后的代码是
<!doctype html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js" />
<script>
var x = 0;
var y = 0;
var z = 0;
function calc(obj) {
var e = obj.id.toString();
if (e == 'tb1') {
x = Number(obj.value);
y = Number(document.getElementById('tb2').value);
} else {
x = Number(document.getElementById('tb1').value);
y = Number(obj.value);
}
z = x + y;
var string = numeral(z).format('0,0');
document.getElementById('total').value = string;
}
</script>
</head>
<body>
<form name="addem" action="" id="addem" >
<p><input type="text" id="tb1" name="tb1" onkeyup="calc(this)"/>1</p>
<p><input type="text" id="tb2" name="tb2" onkeyup="calc(this)"/>2</p>
<input type="text" id="total" name="total" />
</form>
</body>
</html>