2012-02-27 119 views
3

我有一個自動填充字段,並將其源設置爲一組朋友。如何將多維數組傳遞給jquery ui自動完成?

GetFollowing(9, function(response) 
        { 
         if (response) 
         { 
          donor.following = response; 
          for (var i = 0; i < response.length -1; i++) 
          { 
           // make sure we have an image to display 
           if (response[i].image != null) 
           { 
            $("#following").prepend("<li title='" + response[i].firstname + " " + response[i].lastname + 
             "'><img src='<?php echo $baseUrl;?>/uploads/profile-pictures/" + response[i].image + 
             "' alt='" + response[i].firstname + " " + response[i].lastname + "'/></li>"); 

            // add to the friends array 
            if (! $.inArray(response[i].firstname + " " + response[i].lastname, friends)) 
             friends.push(response[i].firstname + " " + response[i].lastname); 
           } 
          } 
         } 
        }); 
        GetFollowers(9, function(response) 
        { 
         if (response) 
         { 
          donor.followers = response; 
          for (var i = 0; i < response.length -1; i++) 
          { 
           // make sure we have an image to display 
           if (response[i].image != null) 
           { 
            $("#followers").prepend("<li title='" + response[i].firstname + " " + response[i].lastname + 
             "'><img src='<?php echo $baseUrl;?>/uploads/profile-pictures/" + response[i].image + 
             "' alt='" + response[i].firstname + " " + response[i].lastname + "'/></li>"); 

            // Add to the friends array 
            friends.push(response[i].firstname + " " + response[i].lastname); 
           } 
          } 
         } 
         // setup jquery-ui autocomplete 
         $("#msg-to").autocomplete({source: friends}); 
        }); 

我的問題是,我需要一個id與所選擇的價值,如何才能做到這一點相關聯?

回答

5

http://jqueryui.com/demos/autocomplete/

你應該閱讀概述更多。

預期的數據格式

從本地數據,URL或回調的數據可以有兩種 變種:

  • 字符串數組:

[ "Choice1", "Choice2" ]

  • 與 標籤和值的屬性的對象Array:

[ { label: "Choice1", value: "value1" }, ... ]

標籤屬性被顯示在建議菜單。在用戶從菜單中選擇一些內容後,該值將被插入到輸入元素中。如果只指定了一個屬性,它將用於兩個,例如。如果您僅提供值屬性,則該值也將用作標籤。

使用標籤進行文本表示,選擇中每個項目的id值。

您還應該區分數據版本(包含id的隱藏輸入)的可視版本(顯示文字)所使用的文本框 - 類似於日期選擇器小部件(又名備用數據)的文本框。這可以使用這些設置進行操作。

相關問題