1

如果您使用谷歌瀏覽器訪問我的網站http://djdavid98.zymichost.com/.tagguide/,並嘗試使用搜索框(例如,鍵入'a',點擊第一個選項),您會看到在控制檯中, jQuery記錄了一個Uncaught TypeError: Cannot call method 'toLowerCase' of undefined。我不明白爲什麼。jQuery使用typeahead更新參數

的問題必須在這部分代碼,因爲刪除它修復一切:

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'); 
     ... 
    }, 
}); 

回答

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替換爲$(this).val()的回調參數,該錯誤將引發錯誤。

0

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; 
    }, 
}); 

看到 - 你在$('#findpony')。attr('placeholder')。attr('title')後面調用#(this)

+0

改變了順序,仍然沒有。 – SeinopSys

+0

嘗試使用$('#findname')而不是$(this) –

+0

爲什麼'item'沒有被使用?這是從回調中發送的內容。並且這些選擇器沒有任何鏈接的「父()」功能是有道理的。 –