javascript 无法读取未定义的属性“toLowerCase” - jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30043071/
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
Cannot read property 'toLowerCase' of undefined - jQuery
提问by Frank
I've read the other examples where toLowerCase
is undefined but I still don't understand. Can anyone explain why my function won't work?
我已经阅读了toLowerCase
未定义的其他示例,但我仍然不明白。谁能解释为什么我的功能不起作用?
var changeTitle = function() {
var loanTitle = $(this).val();
$(this).prev().text(loanTitle);
};
<h2>Loan One</h2>
<input type="text" onkeypress="changeTitle()" maxlength="25" />
回答by Hoyen
In your function, the this
object refers to the window and not the input field. Update your onkeypress
code to the following:
在您的函数中,this
对象是指窗口而不是输入字段。将您的onkeypress
代码更新为以下内容:
<input type="text" name="loanName" onkeypress="changeTitle.call(this)" class="loanNameV1 qForms" maxlength="25" placeholder="Loan Name" />
回答by ThisClark
I'm not sure what you really are trying to do, but I passed your input element as a parameter to the function you defined as this
and I added the JQuery library to my snippet. It works with the exception that the last key stroked isn't displayed in the title. Maybe you can use this along with your toLowerCase feature and fix it up to your liking.
我不确定您真正要做什么,但我将您的输入元素作为参数传递给您定义的函数,this
并将 JQuery 库添加到我的代码段中。它的工作原理是最后一次敲击的键不显示在标题中。也许您可以将它与 toLowerCase 功能一起使用,并根据自己的喜好进行修复。
var changeTitle = function(obj) {
var loanTitle = $(obj).val();
$(obj).prev().text(loanTitle);
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h2 class="nameLoan1"> Loan One </h2>
<input type="text" onkeypress="changeTitle(this);" maxlength="25" placeholder="Loan Name" />