1

如果您使用谷歌浏览器访问我的网站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');
        ...
    },
});
4

2 回答 2

1

它实际上在缩小的 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 控制台在定位错误时有时会“丢失”它的位置;他们很快就会在同一个地方找到。您只需手动刷新路径即可将其重置。

item结果证明,解决方案是从引发错误的回调参数 for替换的两倍$(this).val()

于 2013-01-01T02:16:37.790 回答
0

jquery中的错误是由于调用.val()未定义而引发的

它的这条线

$('#ponies li:contains('+$(this).val()+')').parent().parent().parent().addClass('in');

尝试

$('#ponies li:contains(a)').parent().parent().parent().addClass('in');

如果您只在文本框中输入“a”,这将模拟值是什么,如果可行,那么您需要获取 .val()$('#findname')而不是$(this)

进一步阅读该代码我相信$(this)返回的原因是因为它在此代码中的undefined2 之后被调用:.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;
    },
});

看 - 你在 $('#findpony').attr('placeholder').attr('title') 之后调用 #(this)

于 2013-01-01T02:07:21.247 回答