2012-03-01 48 views
62

我正在使用以下代碼將我的jQuery UI自動完成項目呈現爲HTML。 項目在自動完成控件中正確呈現,但我不斷收到此javascript錯誤,無法移過它。無法使用HTML設置未定義的jQuery UI自動完成屬性'_renderItem'

火狐無法轉換JavaScript參數

無法設置未定義

donor.GetFriends(function (response) { 
    // setup message to friends search autocomplete 
    all_friends = []; 
    if (response) { 
     for (var i = 0; i < response.all.length - 1; i++) {     
       all_friends.push({ 
        "label":"<img style='padding-top: 5px; width: 46px; height: 46px;' src='/uploads/profile-pictures/" + 
         response.all[i].image + "'/><br/><strong style='margin-left: 55px; margin-top: -40px; float:left;'>" + 
         response.all[i].firstname + " " + response.all[i].lastname + "</strong>", 

        "value":response.all[i].firstname + " " + response.all[i].lastname, 
        "id":response.all[i].user_id}); 
      } 
     }   

    $('#msg-to').autocomplete({ 
     source:all_friends, 
     select:function (event, ui) {    
      // set the id of the user to send a message to 
      mail_message_to_id = ui.item.id; 
     } 

    }).data("autocomplete")._renderItem = function (ul, item) { 
     return $("<li></li>") 
      .data("item.autocomplete", item) 
      .append($("<a></a>").html(item.label)) 
      .appendTo(ul); 
    }; 
}); 

不確定的特性「_renderItem」爲什麼它是引發此錯誤,或者我必須做什麼通過它...任何幫助表示讚賞。

+0

頁面上是否有Id##msg-to'的元素? – 2012-03-01 17:22:42

+0

是的,有... $(「#msg-to」)是.autocomplete綁定到的文本輸入字段。 – 2012-03-02 01:49:35

+1

這有助於:http://stackoverflow.com/questions/5590776/why-am-i-getting-this-js-error – Ilmars 2012-05-31 07:43:23

回答

175

因爲我剛剛加入並不能drcforbin的帖子評論上面,我想我必須添加我自己的答案。

drcforbin是正確的,雖然它是一個真正與OP不同的問題。由於剛剛發佈的新版jQuery UI,現在任何人都可能面臨此問題。有關自動完成一定的命名約定在jQuery用戶界面被棄用V1.9,並在1.10被完全刪除(見http://jqueryui.com/upgrade-guide/1.10/#autocomplete)。

什麼是混亂的,但問題在於,他們只提從item.autocomplete數據標籤UI的自動完成項目過渡,但自動完成數據標籤也被改名爲用戶界面 - 自動完成。而且它更令人困惑,因爲演示仍在使用舊的語法(因此被破壞)。

下面是什麼需要在自定義數據的_renderItem功能jQuery UI的1.10.0演示此處更改:http://jqueryui.com/autocomplete/#custom-data

原始代碼:

.data("autocomplete")._renderItem = function(ul, item) { 
    return $("<li>") 
    .data("item.autocomplete", item) 
    .append("<a>" + item.label + "<br>" + item.desc + "</a>") 
    .appendTo(ul); 
}; 

固定碼:

.data("ui-autocomplete")._renderItem = function(ul, item) { 
    return $("<li>") 
    .data("ui-autocomplete-item", item) 
    .append("<a>" + item.label + "<br>" + item.desc + "</a>") 
    .appendTo(ul); 
}; 

注意改變兩個自動完成item.autoco完整。我已經證實,這在我自己的項目中起作用。

+1

+1,謝謝。我在第二段中提到了確切的問題。問題解決了! – 2013-01-25 16:26:53

+0

很好的解釋...一直在尋找像這樣詳細的東西。謝謝 – Greg 2013-10-03 18:44:57

+0

偉大的解決方案。完全爲我工作! – user1429980 2014-04-23 22:41:20

25

我碰到了同樣的問題...似乎在以後的版本中,它必須是.data("ui-autocomplete")代替.data("autocomplete")

+2

我沒有看到區別 – mac10688 2013-01-20 04:18:24

+2

謝謝,這爲我解決了問題 – Tomba 2013-01-25 12:02:48

1

根據jQuery UI的版本你使用它要麼是「自動完成」或「UI-自動完成」,我做了這個更新的組合框插件來解決這個問題(LN〜78-85)

var autoComplete = input.data("ui-autocomplete"); 
if(typeof(autoComplete) == "undefined") 
    autoComplete = input.data("autocomplete"); 

autoComplete._renderItem = function(ul, item) { 

...

6

我使用jQuery 1.10.2和它的工作使用:

.data("custom-catcomplete")._renderItem = function(ul, item) { 
    return $("<li>") 
    .data("ui-autocomplete-item", item) 
    .append("<a>" + item.label + "<br>" + item.desc + "</a>") 
    .appendTo(ul); 
}; 
+0

謝謝你,你的代碼幫助我嘗試了無盡的選擇後^^ – azzy81 2016-06-30 10:38:59

5

而現在,jQuery的2.0。0,這是你的新模塊的名稱,但替換「。」。 (dot)by「 - 」(破折號):

jQuery.widget ('custom.catcomplete', jQuery.ui.autocomplete, { 
    '_renderMenu': function (ul, items) { 
     // some work here 
    } 
}); 

$this.catcomplete({ 
    // options 
}).data('custom-catcomplete')._renderItem = function (ul, item) {} 
3

爲了任何人在這篇文章中絆倒而發帖。

如果您沒有將.autocomplete放入文檔就緒事件中,該錯誤也會顯現出來。

下面的代碼將失敗:

<script type="text/javascript"> 
    $('#msg-to').autocomplete({ 
     source:all_friends, 
     select:function (event, ui) {    
      // set the id of the user to send a message to 
      mail_message_to_id = ui.item.id; 
     } 

    }).data("autocomplete")._renderItem = function (ul, item) { 
     return $("<li></li>") 
      .data("item.autocomplete", item) 
      .append($("<a></a>").html(item.label)) 
      .appendTo(ul); 
    }; 
</script> 

,而下面的代碼將工作:

<script type="text/javascript"> 
    $(function(){ 
     $('#msg-to').autocomplete({ 
      source:all_friends, 
      select:function (event, ui) {    
       // set the id of the user to send a message to 
       mail_message_to_id = ui.item.id; 
      } 

     }).data("autocomplete")._renderItem = function (ul, item) { 
      return $("<li></li>") 
       .data("item.autocomplete", item) 
       .append($("<a></a>").html(item.label)) 
       .appendTo(ul); 
     }; 
    }); 
</script> 
+0

這對我的骨幹應用程序工作 – jakeed1 2014-01-12 18:22:49

+0

這是我的「無法設置財產'_renderItem'未定義的Jasmine規範 – 2014-03-21 19:41:26

7

我知道我有我的回答來晚了,但如果人們對未來仍然不得到

 
.data("ui-autocomplete-item", item) 

工作再嘗試這種insted的

$(document).ready(function(){ 
$('#search-id').autocomplete({ 
    source:"search.php", 
    minLength:1,  
    create: function() { 
    $(this).data('ui-autocomplete')._renderItem = function (ul, item) { 
     return $('<li>') 
     .append("<a>" + item.value + ' | ' + item.label + "</a>") 
     .appendTo(ul); 
    }; 
    } 
}) 
}); 

它爲我和我是有登錄的Funktion問題..我無法登錄,因爲它說

 
Uncaught TypeError: Cannot set property '_renderItem' of undefined 

希望這不會幫助別人:)

/卡欣

+0

加上(+)後item.label缺少。 – Ali 2015-05-18 02:31:00

+0

@ShafaqatAli謝謝! – Kahin 2015-05-19 07:52:30

相關問題