0

我目前在下面有一个javascript 代码,它在处理货币格式(即500.00 美元)时将总计计算为只读文本字段。我遇到的问题是,当以货币格式 (MinusSignNegative) 即 ($500.00) 而不是 -$500.00 输入多个负数时,如何处理计算。我目前在总计中遇到 NaN 错误。

我相信这个正则表达式应该处理它,但我不知道如何实现。http://www.regexlib.com/(X(1)A(bk8AHOFYowt7XHOC4WUCtfdM2LhlaovTNInhWLTrzAeoeq-c53XkkdwLD-WDe3OgQtJ7BLHSs0P-u-RrLbfVZaQIHkBH2exYGw0qtz6nqSamZNVqtnyufo9Y3nrEq5mq-mry63HY4Nnv0dfsQOZzKvuwcKAuwigyyQva-67laxr-ModxTQESW8fXx2XJL_0L0))/REDetails.aspx?regexp_id=625&AspxAutoDetectCookieSupport=1

任何人都可以提供解决方案吗?

    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function total(what,number) {
        var grandTotal = 0;
        for (var i=0;i<number;i++) {
           if (what.elements['price' + i].value.replace(/\$|\,/g,'') == '')
               what.elements['price' + i].value.replace(/\$|\,/g,'') == '0.00';
           grandTotal += (what.elements['price' + i].value.replace(/\$|\,/g,'') - 0);
        }
        what.grandTotal.value = (Math.round(grandTotal*100)/100);
        }
    //-->
    </SCRIPT>

    <FORM NAME="myName">
    Tax Due/Refund: <input TYPE="text" NAME="price0" VALUE="" SIZE="10" class='currency' onChange="total(this.form,3)"><BR>
    Interest: <input TYPE="text" NAME="price1" VALUE="" SIZE="10" class='currency' onChange="total(this.form,3)"><BR>
    Penalty: <input TYPE="text" NAME="price2" VALUE="" SIZE="10" class='currency' onChange="total(this.form,3)"><BR>
    Total Amount Assessed: <INPUT TYPE="TEXT" NAME="grandTotal" class='currency' SIZE="25" READONLY="readyonly" style="background:#eee none; color:#222; font-weight:bold">
    </FORM>
4

2 回答 2

0

如果您尝试将负号从数字移到美元符号的左侧,请在写入总值字段时执行此操作。

function total(what, number){
    var grandTotal= 0,  i= number, val, sign;
    while(i){
        val= what.elements['price' + i--].value.replace([$, ]+/g,'') ;
        grandTotal+= parseFloat(val) || 0;
    }
    sign= grandTotal<0? '-' :'';
    what.grandTotal.value=  sign+'$'+ Math.abs(Math.round(grandTotal*100)/100);
}
于 2012-06-13T15:53:17.057 回答
0

您可以使用或不使用该正则表达式来做到这一点。

没有:

fieldValue = field.value; // "(500.00)"
// search for a "(" char
if (fieldValue.indexOf("(") >= 0) {
    // remove all chars, but numbers and dots
    fieldValue = fieldValue.replace(/[^0-9.]/ig, "");
    // 500.00
    numberFieldValue = Number(fieldValue) * -1;
}

和:

fieldValue = field.value; // "(500.00)"
// test if the value matches that pattern for negative numbers
if (fieldValue.match(/PUT_THAT_REGEX_HERE/g)) {
    // remove all chars, but numbers and dots
    fieldValue = fieldValue.replace(/[^0-9.]/ig, "");
    // 500.00
    numberFieldValue = Number(fieldValue) * -1;
}

它应该是这样的:

function total(what,number) {
    var grandTotal = 0;
    for (var i=0;i<number;i++) {

        fieldValue = what.elements['price' + i].value; // "(500.00)" 
        // search for a "(" char 
        if (fieldValue.indexOf("(") >= 0) { 
            // remove all chars, but numbers and dots 
            fieldValue = fieldValue.replace(/[^0-9.]/ig, ""); 
            // 500.00 
            numberFieldValue = Number(fieldValue) * -1; 
        } else if (fieldValue.replace(/\$|\,/g,'') == '') {
            numberFieldValue = 0;
        } else {
            numberFieldValue = number(fieldValue.replace(/\$|\,/g,''));
        }

        grandTotal += numberFieldValue ;
    }
    what.grandTotal.value = (Math.round(grandTotal*100)/100);
}
于 2012-06-13T15:41:15.547 回答