0

我一直在使用一个简单的表格来计算某人下订单的总金额。我正在为表单使用预设框架,该框架使用输入文本来确定项目的数量。

出于某种原因,当我访问该值,将其转换为字符串,然后调用它的长度时,我收到错误“SCRIPT438:对象不支持属性或方法'长度'”。当我对值的类型执行 console.log() 时,我确实得到了一个字符串。

为什么我会收到此错误?

如果不清楚,我只是在寻找解决此错误的方法,而不是代码批评。

控制台文本:LOG:a,small:2 LOG:textVal:2 LOG:textVal 类型:字符串 SCRIPT438:对象不支持属性或方法“长度”第 130 行

function numOnly(a){//used to check that only a number has been inputted into the quantity box
var textVal = document.getElementById(a).value.toString();
console.log("textVal: "+textVal);
console.log("type of textVal: "+typeof textVal);
var returnVal = '';
for(var i = 0;i<textVal.length();i++){
    if(textVal.charAt(i)=='0'||textVal.charAt(i)=='1'||textVal.charAt(i)=='2'||textVal.charAt(i)=='3'||textVal.charAt(i)=='4'||textVal.charAt(i)=='5'||textVal.charAt(i)=='6'||textVal.charAt(i)=='7'||textVal.charAt(i)=='8'||textVal.charAt(i)=='9'){
        returnVal+=textVal.charAt(i);
    }//if
}//for
console.log("a after loop: "+a);
return returnVal;
function numOnly(a){//used to check that only a number has been inputted into the quantity box WHY DOES IT GET AN OBJECT WINDOW
var textVal = '';
textVal = document.getElementById(a).value.toString();
console.log("textVal: "+textVal);
console.log("type of textVal: "+typeof textVal);
var returnVal = '';
for(var i = 0;i<textVal.length();i++){
    if(textVal.charAt(i)=='0'||textVal.charAt(i)=='1'||textVal.charAt(i)=='2'||textVal.charAt(i)=='3'||textVal.charAt(i)=='4'||textVal.charAt(i)=='5'||textVal.charAt(i)=='6'||textVal.charAt(i)=='7'||textVal.charAt(i)=='8'||textVal.charAt(i)=='9'){
        returnVal+=textVal.charAt(i);
    }//if
}//for
console.log("a after loop: "+a);
return returnVal;
}//numOnly()

我在哪里调用函数

case "S":
a=document.getElementById(sizeId[i]);
console.log("a, small: "+a.value);
if(a!=null) SSTotal+=(parseInt(numOnly(sizeId[i]))*prices[1]);
break;
4

1 回答 1

2

长度是一个属性而不是一个方法,所以试试:

textVal.length

代替

textVal.length()

此外,您不需要对toString()值进行操作。它本身就是字符串。

于 2013-07-09T14:58:29.663 回答