Uncaught TypeError: Cannot call method 'replace' of undefined

社会主义新天地 提交于 2020-01-03 17:44:29

问题


$(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?


回答1:


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('$', ''));


来源:https://stackoverflow.com/questions/8918009/uncaught-typeerror-cannot-call-method-replace-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!