0
我寫一些代碼對某些功能加入2號和顯示結果與逗號
我的第一個文本框
<asp:TextBox ID="txtLeasePaymentFixed" type="text" runat="server" class="number" CssClass="form-control" Style="text-align: right" TabIndex="101" placeholder="e.g 123,456,789" onKeyUp="fncCalculateSum()" onBlur="fncCalculateSum()"></asp:TextBox>
我的第二個文本框是
<asp:TextBox ID="txtLeasePaymentVairable" type="text" runat="server" CssClass="form-control" Style="text-align: right" TabIndex="102" placeholder="e.g 123,456,789" onKeyUp="fncCalculateSum()" onBlur="fncCalculateSum()"></asp:TextBox>
標籤哪Toltal表明是這樣的
<asp:Label ID="lbltotal" runat="server" Text=""></asp:Label>
函數調用的onblur和的onkeyup是這樣的:
function fncCalculateSum() {
var LeasePaymentFixed = $('#MainContent_txtLeasePaymentFixed');
var LeasePaymentVariable = $('#MainContent_txtLeasePaymentVairable');
if (LeasePaymentFixed.val() == '' || LeasePaymentFixed.val() == null) {
PaymentFixed = 0;
}
else {
PaymentFixed = parseFloat(LeasePaymentFixed.val());
}
if (LeasePaymentVariable.val() == '' || LeasePaymentVariable.val() == null) {
PaymentVariable = 0;
}
else {
PaymentVariable = parseFloat(LeasePaymentVariable.val());
}
(Sum) = PaymentFixed + PaymentVariable
if ((LeasePaymentFixed.val().trim() == '' && LeasePaymentVariable.val().trim() == '') || (LeasePaymentFixed.val() == null && LeasePaymentVariable.val() == null)) {
$("#MainContent_lbltotal").text('');
}
else {
$("#MainContent_lbltotal").html(parseFloat(Sum));
}
}
現在我使用jQuery代碼,每3個數字這樣
$('#MainContent_txtLeasePaymentFixed,#MainContent_txtLeasePaymentVairable,#MainContent_lbltotal').keyup(function(event){
// skip for arrow keys
if(event.which >= 37 && event.which <= 40){
event.preventDefault();
}
var $this = $(this);
var num = $this.val().replace(/,/gi, "").split("").reverse().join("");
var num2 = RemoveRougeChar(num.replace(/(.{3})/g,"$1,").split("").reverse().join(""));
console.log(num2);
// the following line has been simplified. Revision history contains original.
$this.val(num2);
});
function RemoveRougeChar(convertString){
if(convertString.substring(0,1) == ","){
return convertString.substring(1, convertString.length)
}
return convertString;
}
後適用於國際格式的逗號。當我寫在第一個文本數字它帶有逗號的框,沒關係。 同樣在第二個文本框中寫入,但它不顯示標籤上所有數字的總和。
謝謝我來了知道,逗號不能在parseflot,但我怎麼可以傳遞結果,然後將其轉換爲解析浮動後 –
我可能會存儲數值的值(格式化之前)在[DOM元素數據集](http://html5demos.com/dataset)(例如'el.dataset.value = number;')。這樣您可以更容易地獲得原始值。 –