简体   繁体   English

未捕获的TypeError:无法读取未定义的属性“ toLowerCase”

[英]Uncaught TypeError: Cannot read property 'toLowerCase' of undefined

$('#sum').keydown(function(){
           updateResultPrice(); 
        });

        function updateResultPrice() {
            ajax('/payment/summ', 'price='+$(this).val());
        }

Not working! 不工作! Console log print: Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 控制台日志打印:未捕获的TypeError:无法读取未定义的属性'toLowerCase'

You don't have a call to .toLowerCase() , but I'm guessing you're chaining it to the end of .val() . 您没有对.toLowerCase()的调用,但是我猜您正在将其链接到.val()的末尾。

The trouble is that your this value is window , and not the #sum element. 麻烦的是,您的this值是window ,而不是#sum元素。

Change your handler to this: 将您的处理程序更改为此:

$('#sum').keydown(updateResultPrice); // <-- pass the function directly

function updateResultPrice() {
    ajax('/payment/summ', 'price='+$(this).val().toLowerCase());
}

Now when the handler is invoked, this will reference the #sum variable and .val() will not return undefined . 现在,在调用处理程序时, this将参考#sum变量和.val()将不会返回undefined

I tested your code, as is, and didn't actually get the "Uncaught TypeError: Cannot read property 'toLowerCase' of undefined" error through the console. 我按原样测试了您的代码,但实际上没有通过控制台收到“ Uncaught TypeError:无法读取属性'toLowerCase'的未定义”错误。 However, I did manage to trigger an error with the ajax() method. 但是,我确实使用ajax()方法触发了错误。

The reason your code wasn't working, was down to the fact of $(this) would equal the window , and not the #sum element. 您的代码无法正常运行的原因在于$(this)等于window而不是#sum元素。 six fingered man did explain this in his answer. 六个手指的男人的回答确实解释了这一点。

Try using this code instead. 尝试改用此代码。

// Switch 'keydown' to 'on' and include 'keyup' event to get the actual data;
// The 'on' method allows you to "string" events together. 'keyup keydown click focus...' etc.
$('#sum').on('keyup', function(){
    // Define a variable to make calling 'this' easier to write;
    var me = $(this);
    // Get the value of "me";
    var val = me.val();

    // Relay the value to the function;
    updateResultPrice( val );
});

// The function, updateResultPrice, accepts an argument of "value";
function updateResultPrice( value ) {
    // Your prior code used $(this).val() within the function;
    // The function doesn't have a $(this) to retreive the value from it,
    // So, use the "value" argument;
    $.ajax('/payment/summ', 'price=' + value); // Call "$.ajax", not "ajax";
    // The above snippet will trigger a 404, should the file not exist.

    // Just to test that it works, log it to the console;
    console.log( 'the price is: '+value );
}

For your testing pleasures, here's a JSFiddle demo of the above code. 为了您的测试愉快,这里是上述代码的JSFiddle演示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:无法读取属性toLowerCase的未定义 - Uncaught TypeError: Cannot read property toLowerCase of undefined 未捕获的TypeError无法读取未定义的属性“ toLowerCase” - Uncaught TypeError Cannot read property 'toLowerCase' of undefined 未捕获的TypeError:无法读取未定义的属性'toLowerCase' - Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 我有错误未捕获的TypeError:无法读取未定义的属性&#39;toLowerCase&#39; - I have the error Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 未捕获的TypeError:无法在文本字段上读取未定义错误的属性“ toLowerCase” - Uncaught TypeError: Cannot read property 'toLowerCase' of undefined Error on text field 未捕获的类型错误:无法读取未定义的属性“toLowerCase”(待办事项列表应用程序) - Uncaught TypeError: Cannot read property 'toLowerCase' of undefined (Todo list application ) Elastic_Grid | “未捕获的TypeError:无法读取未定义的属性&#39;toLowerCase&#39;” - Elastic_Grid | “Uncaught TypeError: Cannot read property 'toLowerCase' of undefined” 获取错误未捕获的TypeError:无法读取未定义的属性“ toLowerCase” - Getting error Uncaught TypeError: Cannot read property 'toLowerCase' of undefined jsonp请求上的“ Uncaught TypeError:无法读取未定义的属性&#39;toLowerCase&#39;” - “Uncaught TypeError: Cannot read property 'toLowerCase' of undefined” on jsonp request TokenInput +未捕获的TypeError:无法读取未定义的属性“ toLowerCase” - TokenInput + Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM