未捕获的TypeError:无法调用未定义的方法'replace' [英] Uncaught TypeError: Cannot call method 'replace' of undefined
本文介绍了未捕获的TypeError:无法调用未定义的方法'replace'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$(this).find("input[name=amount]").val($(this).find("input[name=amount]").val().replace('$', ''));
继续在我的开发人员工具上出现此错误。我只想用'
Keep getting this error on my developer tools. I just want to replace the character $ with nothing which is ''
中的任何内容替换字符$?想法?
Thoughts?
推荐答案
您的错误只是说没有与您的选择器匹配的元素,因此 element.val()
正在返回 undefined
,没有替换
方法。尝试在每一步调试它和 console.log()
。
Your error just says that there is no element that matches your selector, so element.val()
is returning undefined
, which has no replace
method. Try debugging it and console.log()
at each step.
此外,您不需要搜索对于元素两次。只需将其存储在变量中:
Also, you don't need to search for the element twice. Just store it in a variable:
var $input = $(this).find('input[name="amount"]');
$input.val($input.val().replace('$', ''));
这篇关于未捕获的TypeError:无法调用未定义的方法'replace'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文