为什么我会收到一条错误消息:.replace不是函数? [英] Why do I get an error message that .replace is not a function?

查看:129
本文介绍了为什么我会收到一条错误消息:.replace不是函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个函数:
$ b $ pre $ function countLitreKgSums(cProductIds){
var cLitreKgSums = new Array();
var cWeek = 0;
for(i = 0; i< cProductIds.length; i ++){
var cLitreKgSum = 0;
$(#plan_table td [class ='week'])。each(function(){
cWeek = $(this).html();
var cLitreKgValue = $( input [name * ='plan_table_week+ cWeek +_prod+ cProductIds [i] +_'])。val();
if(cLitreKgValue ==){
cLitreKgValue = 0;
}
cLitreKgValue = cLitreKgValue.replace(/,/ g,'。')。replace(/ [^ \d\。] / g,'').replace(/ \s / g,'');
cLitreKgValue = parseFloat(cLitreKgValue);
cLitreKgSum + = cLitreKgValue;
});
cLitreKgSum = Math.round(cLitreKgSum * 100)/ 100;
cLitreKgSums [i] = cLitreKgSum;
}
返回cLitreKgSums;
//console.log(cLitreKgSums);
}

我得到的错误信息是.replace不是函数,但是在其他函数中它按原样工作。有什么不同?

解决方案

cLitreKgValue 指向你尝试调用替换的地方,而不是字符串。在这种情况下,错误是正确的 - 数字没有替换方法。


I have this function:

function countLitreKgSums(cProductIds){
  var cLitreKgSums = new Array();
  var cWeek = 0;
  for(i=0;i<cProductIds.length;i++){
    var cLitreKgSum = 0;
    $("#plan_table td[class='week']").each(function(){
            cWeek = $(this).html();
            var cLitreKgValue = $("input[name*='plan_table_week" + cWeek + "_prod" + cProductIds[i] + "_']").val();
            if (cLitreKgValue == "") {
                cLitreKgValue = 0;
            }
            cLitreKgValue = cLitreKgValue.replace(/,/g, '.').replace(/[^\d\.]/g, '').replace(/\s/g, '');
            cLitreKgValue = parseFloat(cLitreKgValue);
            cLitreKgSum += cLitreKgValue;
       });
       cLitreKgSum = Math.round(cLitreKgSum * 100) / 100;
       cLitreKgSums[i] = cLitreKgSum;
  }
  return cLitreKgSums;
  //console.log(cLitreKgSums);
}

I get error message that .replace is not a function but in other functions it works as it should. What is the difference?

解决方案

cLitreKgValue might be a number at the point where you try to call replace on it, not a string. In which case, the error is correct - numbers don't have a replace method.

这篇关于为什么我会收到一条错误消息:.replace不是函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆