2012-01-08 70 views
0

我知道這個問題已被覆蓋,但沒有一個解決方案似乎適用於我。這是我的代碼JQuery自動完成檢索ID

var dialogForm = getDialogForm(); 

    var $dialog = $('<div></div>').html(dialogForm) 


    .dialog 
    ({ 
     autoOpen:  false, 
     title:   "New Message", 
     draggable:  false, 
     modal:   true, 
     dialogClass: "bg_FFF", 
     position:  ["", 120], 
     resizable:  false, 
     width:   440, 
     beforeClose: function(event, ui){ $('.contactsInput').val(""); 
              $('.msgInput').val(""); 
              $(".removeContact").parent().remove(); 
              ignored.length = 0;} 
    }); 


    $('.cancelDialog').live("click", function() 
    { 
     //close dialog 
     $dialog.dialog('close'); 
    }); 


    //send message is clicked 
    $(".sendMsg").live("click", function() 
    { 
     sendMsg(); 
    }); 


    //items to be ingnored 
    var ignored = []; 

    var contactsArray = getUserContacts(); 

    //attach autocomplete 
    $(".contactsInput").autocomplete({ 

    minLength: 1, 

    //define callback to format results 
    source: function(req, add){ 

     //create an empty array 
     var contactsList = []; 

     //process response 
     $.each(contactsArray, function(index, val) 
     { 
      if(ignored.indexOf(val) == -1) 
      { 
       contactsList.push(val); 
      } 
     }); 

     //pass array to callback    
     add($.ui.autocomplete.filter(contactsList, extractLast(req.term))); 

    }, 


    //define select handler 
    select: function(e, ui){ 

     //prevent default action 
     e.preventDefault();alert(ui.item.toSource()); 

     //create formatted contact 
     var contact = ui.item.value, 

     span = $("<span>").text(contact); 

     a = $("<a>").addClass("removeContact").attr({ title: "Remove " + contact }).text("x").appendTo(span); 

     //add contact to contact div 
     span.insertBefore(".contactsInput"); 

     //clear input 
     $(".contactsInput").val("").css("top", 2); 

     ignored.push(ui.item.value) 


    }, 

    //define select handler 
    change: function() { 

     //prevent 'to' field being updated and correct position 
     $(".contactsInput").val("").css("top", 2); 

     } 
    }); 

我試圖檢索autocomplete中的名稱的id。我以格式username-id從數組本地提供數據源。但我得到的是用戶名 - 編號。我需要顯示用戶名,但不是以隱藏格式顯示ID和檢索ID。

由於

回答

0

包含配對值(標籤和值)的一個陣列。標籤是可視化的表示形式,值代表着您的代碼處理。

選擇功能你能,如果他們的數組中指定使用ui.item.valueui.item.label

+0

我有一個數組返回[「cars」,id]但沒有運氣 – JamesBondInBlack 2012-01-08 12:54:00

+0

我需要將我的數組轉換爲不同的格式嗎? – JamesBondInBlack 2012-01-08 12:54:29

+0

你需要發送一個這樣的數組:[{label:'cars',value:id},...] – Marle1 2012-01-08 18:16:18