javascript 类型错误:表达式 'input.replace' [undefined] 的结果不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6343328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:21:00  来源:igfitidea点击:

TypeError: Result of expression 'input.replace' [undefined] is not a function

javascript

提问by Frederik Kammer

I wrote the following function in javascript:

我在javascript中编写了以下函数:

function maskString(input) {
  return input.replace(/\s\s+/," ");
}

Very simple. In a second function I wrote:

很简单的。在第二个函数中,我写道:

function secondFunction(string1, string2) { //Attetion! See Update 2!
  var string1masked = maskString(string1);
  var string2masked = maskString(string2);
  ( ... )
}

The error message is: TypeError: Result of expression 'input.replace' [undefined] is not a function.

错误信息是: TypeError: Result of expression 'input.replace' [undefined] is not a function.

Anybody an idea? Google wasn't really helpful :\

有人有想法吗?谷歌并没有真正的帮助:\

UPDATE 1:
I'm using jQuery and string1 is from an textbox. I'm calling the second function like this:

更新 1:
我正在使用 jQuery,而 string1 来自文本框。我正在像这样调用第二个函数:

var bool = secondFunction (textarea1.val(), textarea2.val()); //Attetion! See Update 2!

UPDATE 2:
I was wrong with the second function ... It's:

更新 2:
我对第二个功能有误......它是:

function secondFunction(string1, array1) {
  var string1masked = maskString(string1);
  var array1masked = maskString(array1);
  ( ... )
}

So my function doesn't work with the array. Unfortunately I have no idea how to change it :(

所以我的函数不适用于数组。不幸的是我不知道如何改变它:(

采纳答案by Lekensteyn

I guess you have code like this:

我猜你有这样的代码:

var textarea1 = $("#textarea1");

textarea1.val()returns undefinedif the element is not found (i.e. an element with ID textarea1). Check if you haven't made a typo in the element selector and if the function is called after the element is available.

textarea1.val()返回undefined如果未找到该元件(即,具有ID的元素textarea1)。检查您是否在元素选择器中没有打错字,以及是否在元素可用后调用该函数。

This function works for strings and arrays containing strings. If an argument or array value is not a string nor an array, it does not touch the value.

此函数适用于字符串和包含字符串的数组。如果参数或数组值既不是字符串也不是数组,则不会触及该值。

function maskData(input) {
    // edit strings
    if (typeof input == "string") return input.replace(/\s\s+/, "");

    // if 'input' is an array, iterate through its elements and pass it to maskData again
    if (input instanceof Array) {
        for (var i=0; i<input.length; i++) {
           input[i] = maskData(input[i]);
        }
        return input;
    }
    // otherwise just return itself untouched
    return input;
    // alternative: return an empty string
    //return "";
}

If your intention is to turn multiple whitespace to a single one (as in string with multiple spaces in it-> string with multiple spaces in it), you could also use the simplified RE:

如果您打算将多个空格转换为单个空格(如string with multiple spaces in it-> string with multiple spaces in it),您还可以使用简化的 RE:

/\s+/g

A single whitespace character will be replaced by a single space. Multiple whitespace characters will also be replaced by a single space. Note that I added the gflag so the replacement occurs multiple times.

单个空格字符将被单个空格替换。多个空白字符也将被单个空格替换。请注意,我添加了g标志,因此替换发生多次。

回答by Kon

I assume inputis your input textbox or something?

我假设input是你的输入文本框还是什么?

In which case, input doesn't have a replace()function defined for it. You want to make sure you're calling replace()on a string - the value inside the input, not the input itself. Try this:

在这种情况下, input 没有replace()为其定义函数。您要确保调用replace()的是字符串 - 输入中的值,而不是输入本身。试试这个:

function maskString(input) {
  return input.value.replace(/\s\s+/," ");
}