var.replace不是一个函数 [英] var.replace is not a function

查看:484
本文介绍了var.replace不是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码尝试修剪Javascript中的字符串,但我收到了标题中提到的错误:

I'm using the below code to try to trim the string in Javascript but am getting the error mentioned in the title:

function trim(str) {
    return str.replace(/^\s+|\s+$/g,'');
}

修改:

我解决了这个问题....对不起我应该把代码放在我如何调用它上面....意识到我不小心传递了表单字段本身的对象而不是它的值。

I fixed the problem.... sorry I should have put the code on how I was calling it too.... realized I accidentally was passing the object of the form field itself rather than its value.

推荐答案

我的猜测是调用 trim 函数的代码是实际上是将字符串传递给它。

My guess is that the code that's calling your trim function is not actually passing a string to it.

如果您发布该代码,我可以更新我的答案。

If you post that code, I could update my answer.

这篇关于var.replace不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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