javascript 无法调用未定义的方法“toLowerCase”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12609059/
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-26 16:39:31  来源:igfitidea点击:

Cannot call method 'toLowerCase' of undefined

javascriptjquery

提问by Scott Selby

I have this code , it is reading the value from a text box, I thought checking for '' before trying to parseInt() would be safe , but apparently not.

我有这个代码,它从文本框中读取值,我认为在尝试 parseInt() 之前检查 '' 是安全的,但显然不是。

I am getting the error:

我收到错误:

Uncaught TypeError: Cannot call method 'toLowerCase' of undefined

未捕获的类型错误:无法调用未定义的方法“toLowerCase”

With this code:

使用此代码:

            var total = 0;
            $.each('.reg-input', function () {
                if ($(this).val() == '') {
                }
                else {
                    total += parseInt($(this).val());  //this line throwing error
                }                    
            });
            if (total == 0) {
                $('.RegisterContainer').hide();                    
            }

回答by Kevin B

'.reg-input'is a string, thisin that case will also be a string, not a dom element. Try this instead:

'.reg-input'是一个字符串,this在这种情况下也将是一个字符串,而不是一个 dom 元素。试试这个:

$('.reg-input').each(function(){...

回答by Guffa

The reason that you get the strange error is that you are calling $.eachwith a string instead of a collection.

您收到奇怪错误的原因是您正在$.each使用字符串而不是集合进行调用。

A string is an object, so it's still a collection, but the items in the collection is the properties and methods of the string class (and toLowerCaseis one of them).

字符串是一个对象,所以它仍然是一个集合,但集合中的项是字符串类的属性和方法(并且toLowerCase是其中之一)。

To loop through the elements that you find with a selector, you use the eachmethodinstead of the $.eachmethod:

要循环使用选择器找到的元素,请使用each方法而不是$.each方法

$('.reg-input').each(function(){
  ..
});

Now thisinside the loop will be an element, not a member of the string class, so you can use $(this).val()to get the values.

现在this循环内部将是一个元素,而不是字符串类的成员,因此您可以使用它$(this).val()来获取值。

回答by Cfrim

I had the exact same problem, and I solved it by assigning $(this).val()to a variable and then parse the variable to an integer. So a correct workaround would be:

我遇到了完全相同的问题,我通过分配$(this).val()给一个变量然后将该变量解析为一个整数来解决它。所以正确的解决方法是:

var total = 0;
        $.each('.reg-input', function () {
            if ($(this).val() == '') {
            }
            else {
                $this_val = $(this).val();
                total += parseInt($this_val);  //this line throwing error
            }                    
        });
        if (total == 0) {
            $('.RegisterContainer').hide();                    
        }

回答by JLeslie

if ($(this).val() == "")checks to see if the value is the string "" not undefined.
You can use the typeof operator like this if(typeof $(this).val() == 'undefined')

if ($(this).val() == "")检查该值是否是未定义的字符串“”。
您可以像这样使用 typeof 运算符 if(typeof $(this).val() == 'undefined')