2015-06-18 88 views
0

我在女巫中動態創建輸入,我需要添加一個twitter typeahead。將Typeahead動態添加到動態生成的輸入

由於html是動態生成服務器端,輸入標記只能從它的類中選擇,我發現附加它的唯一方法是使用delegate() Jquery函數。

爲了在每個輸入中只附加一個typeahead,我在delegate()之後使用了undelegate()函數。

$(document).delegate(".receivers-input", "click", function() { 
     console.log($(this)); // input.receivers-input.col-md-4.col-sm-4.col-xs-4 
     $(this).undelegate($(this)); 
     $(this).typeahead({ 
      minLength: 0, 
     }, { 
      source: function (query, cb) { 
       var Request = $.ajax({ 
       ...      

這種運作良好,但是當我點擊連接預輸入我有這樣的錯誤消息:

Uncaught TypeError: Cannot read property 'origType' of undefined

這可能是因爲$(this).undelegate($(this));

有誰知道如何防止錯誤或有另一種方式比delegate/undelegate達到我的目標?

回答

0

看起來我搜索功能太多,並沒有看到最簡單的解決方案。 我通過修改選擇實現我的目標......

這是代碼:

$(this).on("click", ".receivers-input:not(.tt-input)", function() { 
     $(this).typeahead({ 
      minLength: 0, 
     }, { 

on像代表結合的作品,它的處理程序綁定到匹配選擇,每一個現有或未來元素。

由於預輸入一些數據添加到它連接到所述輸入(tt-hinttt-input,等...),我只是添加在選擇:not巫與預輸入的類排除元件。

希望這可能有助於某人,有一天。

+0

嘿,我面臨着同樣的問題,你能幫助我....你能幫我嗎?這是我的問題的鏈接..http://stackoverflow.com/questions/39181817/to-delegate-typeahead-on-dynamically-generated-input –

相關問題