繁体   English   中英

无法读取未定义的属性“ toLowerCase”-jQuery

[英]Cannot read property 'toLowerCase' of undefined - jQuery

我读过其他未定义toLowerCase示例,但我仍然不明白。 谁能解释我的功能为什么不起作用?

 var changeTitle = function() { var loanTitle = $(this).val(); $(this).prev().text(loanTitle); }; 
 <h2>Loan One</h2> <input type="text" onkeypress="changeTitle()" maxlength="25" /> 

在您的函数中, this对象引用窗口,而不是输入字段。 将您的onkeypress代码更新为以下内容:

<input type="text" name="loanName" onkeypress="changeTitle.call(this)" class="loanNameV1 qForms" maxlength="25" placeholder="Loan Name" />

我不知道你真的正在尝试做的,但我通过你的输入元素作为参数传递给你定义为函数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" /> 

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM