0
我正在研究HTML5 Web應用,其中我從用戶那裏獲得輸入,並將其轉換爲逗號數字,如1,000,但我想用逗號將它也應轉換爲固定樣1,000.00所以如何在這種方法添加這裏是我的代碼如何將toFixed()應用於HTML5網絡應用中的輸入值
這裏是我的代碼
這裏的jsfiddle演示工作是代碼
<input type="text" id="test" onBlur="addCommas(this)">
<input type="hidden" id="testRealValue">
<script>
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function addCommas(input){
nStr = input.value;
if(!isNumber(nStr))return;
document.getElementById("testRealValue").value = nStr;
var offset = nStr.length % 3;
if (offset == 0)
input.value = nStr.substring(0, offset) + nStr.substring(offset).replace(/([0-9]{3})(?=[0-9]+)/g, "$1,");
else
input.value = nStr.substring(0, offset) + nStr.substring(offset).replace(/([0-9]{3})/g, ",$1");
var a =document.getElementById('testRealValue').value;
alert(a);
}
</script>
@它意味着你的代碼將只顯示到固定值不是逗號我已經做了這個,但是當我加逗號它不起作用可以請你在jsfiddle中編輯,因爲我給了鏈接 – 2013-04-26 06:58:28
更新了我的答案,這裏是小提琴:http://jsfiddle.net/FhZ6y/10/ – Kenneth 2013-04-26 07:01:00