未定义不是函数“”“

时间:2014-12-22 18:00:21

标签: javascript

为什么我会收到未捕获的类型错误:undefined不是函数"""
我的页面中没有J查询

这是我的代码:

function CDec(Currency) {
    var number = Currency.replace(/[^0-9\.]+/g,"");
    return number;
}

调用函数

SellPrice.value = "$" +  CurrencyFormat(CDec(ListPrice.value * (1 + CDec(Markup.value))) * CDec(Mutiplier.value));

Markup.value =   CurrencyFormat(100 * (CDec(SellPrice.value)-CDec(Netcost.value)) / CDec(Netcost.value)) + "%" 

3 个答案:

答案 0 :(得分:2)

只需将作为参数传递的数字转换为字符串,例如:

function CDec(Currency) {
    var number = (Currency + '').replace(/[^0-9\.]+/g,"");
    return number;
}

但我建议您先验证输入数据。

答案 1 :(得分:2)

function CDec(Currency) {
    Currency = Currency.toString();
    var number = Currency.replace(/[^0-9\.]+/g,"");
    return number;
}

答案 2 :(得分:1)

鉴于您的评论,您最好更换已知的可能的字符并验证返回结果。

function CDec(Currency) {
    if (typeof Currency !== "number")
        Currency = parseFloat(Currency.toString().replace(/[£$%]/g, ""));

    return Currency;
}

console.log( CDec(1.22) );
console.log( CDec(-1.22) );
console.log( CDec("$1.22") );
console.log( CDec("$1.22%") );
console.log( CDec("-£1.22") );

if (isNaN(CDec("Woof"))
   alert("invalid!");

您还应该扩展您的值:Precise Financial Calculation in JavaScript. What Are the Gotchas?