jQuery使用typeahead updatater参数抛出typeError
jQuery throws a typeError using typeahead updater parameter
如果您访问我的网站与谷歌浏览器在http://djdavid98.zymichost.com/.tagguide/,并尝试使用搜索框(例如:键入'a',单击第一个选项),您将在控制台中看到,jQuery记录了一个Uncaught TypeError: Cannot call method 'toLowerCase' of undefined
。我不明白为什么。
问题一定在这部分代码中,因为删除它可以修复一切:
tagguide.js(第20-46行):$('#findname').typeahead({
...
updater : function (item) {
$('#ponies li:contains('+$(this).val()+')').parent().parent().parent().addClass('in');
...
},
});
$('#findpony').typeahead({
...
updater : function(item){
$('#'+$('div.accordion-group > div > a.accordion-toggle:contains('+$(this).val()+')').attr('id')).addClass('in');
...
},
});
它实际上位于缩小后的jQuery文件中的一行,箭头指向它:
2148
val: function( value ) {
var hooks, ret, isFunction,
elem = this[0];
if ( !arguments.length ) {
if ( elem ) {
hooks = jQuery.valHooks[ elem.type ] ||
jQuery.valHooks[ elem.nodeName.toLowerCase() ]; //<<<<<<<
if ( hooks && "get" in hooks &&
(ret = hooks.get( elem, "value" )) !== undefined ) {
return ret;
}
ret = elem.value;
return typeof ret === "string" ?
// handle most common string cases
ret.replace(rreturn, "") :
// handle cases where value is null/undef or number
ret == null ? "" : ret;
}
return;
}
2174
我注意到Chrome控制台有时会"失去"它的位置时,定位错误;他们很快就会在同一个地方被发现。你只需要手动刷新路径来重置它。
解决方案是双重的,将$(this).val()
从回调参数中替换为item
,这会抛出错误。
jquery调用未定义的.val()
时抛出错误
就是这一行
$('#ponies li:contains('+$(this).val()+')').parent().parent().parent().addClass('in');
试
$('#ponies li:contains(a)').parent().parent().parent().addClass('in');
将模拟如果你只在文本框中输入"a"的值,如果这有效,那么你需要得到$('#findname')
的。val()而不是$(this)
进一步阅读代码,我认为$(this)
返回undefined
的原因是因为它在代码中的2 .attr()
之后被调用:
$('#findpony').attr('placeholder',function(){
// code removed
}).attr('title',function(){
// code removed
}).typeahead({
source : ponylist,
updater : function(item){
$('#'+$('div.accordion-group > div > a.accordion-toggle:contains('+$(this).val()+')').attr('id')).addClass('in');
return item;
},
});
在$('# findony ').attr('placeholder').attr('title')之后调用#(this)
相关文章:
- 函数参数中的数据与指定变量之间的任何性能差异
- 使用Express捕获参数
- 参数变量出现ngTable指令问题
- AngularJS:我可以跳过函数参数回调吗
- 如何使用skip参数使用angular ui引导进行服务器端分页
- 要求未定义JS回调参数
- 我的jQuery插件参数没有正确启动,遇到了问题
- TypeError:无法读取属性'推'未定义的JavaScript
- 获取@ResponseBody的一部分作为主干和Spring MVC控制器之间的参数
- 当我将参数设置为对象时,JavaScript返回TypeError
- 抛出消息;TypeError:第一个参数必须是字符串或Buffer
- html5拖动&drop-TypeError:Node.appendChild的参数1不是对象
- Grunt:抛出新的TypeError(路径.resolve的参数必须是字符串');
- TypeError:Window.postMessage的参数不足
- Can't .join()函数参数- TypeError: undefined不是函数
- 如何在使用Mocha/Chai的函数中使用某些参数时正确抛出TypeError
- node.js和PostgreSQL query - throw new TypeError('第一个参数必须是
- TypeError:使用Node.js fs.read()的参数不正确
- TypeError:Node.appendChild的参数1未实现接口Node
- jQuery使用typeahead updatater参数抛出typeError