2010-09-20 100 views
-1

我爲每個結果返回多個數據。在每個結果中,我都有不同的鏈接,我想通過這些鏈接進行選擇。現在,無論用戶點擊結果的位置,只是將標題放入文本框而不是處理鏈接。jquery ui自動完成 - 操縱結果

$(function() { 
function log(message) { 
$("<div/>").text(message).prependTo("#log"); 
$("#log").attr("scrollTop", 0); 

}

$.ajax({ 
url: "links2.xml", 
dataType: "xml", 
success: function(xmlResponse) { 
var data = $("ROW", xmlResponse).map(function() { 
    return { 
    value: $("SC_DF_FIELD_1", this).text(), 
    url: $("SC_DF_FIELD_2", this).text(), 
    support_url: $("SC_DF_FIELD_3", this).text(), 
    description: $("SC_DF_FIELD_4", this).text(), 
    contact: $("SC_DF_PERSON_LINK", this).text() 

    }; 
}).get(); 

$("#birds").autocomplete({ 
    source: data, 
    minLength: 0 

}).data("autocomplete")._renderItem = function(ul, item) { 
return $("<li></li>") 
.data("item.autocomplete", item) 
.append("<a>" + item.value + "<br>" + item.url + "<br>" + item.description + "<br>" + "Support URL: " + item.support_url + "<br>" + "Contact: " + "<a href=http://someurl.whatever?p_id=" + item.contact + ">Test</a>" + "</a>") 
.appendTo(ul); 
}; 

} 
}) 

}); 

所以我希望他們能夠點擊item.url它去那裏,或item.contact它去那裏。

編輯:

這是我試用的formatItem代碼。它並不會對返回的內容產生任何影響。

function formatItem(item, foo, bar, term){ 
    var temp = item.title + '<br />&nbsp;&nbsp;' + item.description + '<br />' + '<a href=' + item.url + '>test</a>'; 
    return temp; 

} 


    $.ajax({ 
     url: "links2.xml", 
     dataType: "xml", 
     success: function(xmlResponse) { 
      var data = $("ROW", xmlResponse).map(function() { 
       return { 
        value: $("SC_DF_FIELD_1", this).text(), 
        url: $("SC_DF_FIELD_2", this).text(), 
        support_url: $("SC_DF_FIELD_3", this).text(), 
        description: $("SC_DF_FIELD_4", this).text(), 
        contact: $("SC_DF_PERSON_LINK", this).text() 

       }; 
      }).get(); 

      $("#birds").autocomplete({ 
       source: data, 
       minLength: 0, 
       formatItem: formatItem 

      }) 

     } 
    }) 

}); 

回答

0

禁用click事件處理程序並不困難,並解決了問題。

0

你應該看看 http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions ,特別是formatItemformatResult選項。

使用formatItem選項代替 您的_renderItem hack。有 自動完成行內多個不同的鏈接是多一點 複雜。我建議,也許 你只是使formatResult 功能什麼都不做,並鉤到你的 自定義聯繫鏈接和什麼不通過 .delegate()處理程序。

EHH ....

,你有解決這種情況的原因是自動完成的目的不是要在格式化的列表項的鏈接。如果您查看源代碼,您將看到分配給列表容器的單擊事件處理程序,並且它在列表框內發生的所有單擊中返回false。考慮到這一點,簡單的.delegate()處理程序不會解決問題。每次創建時,您都必須首先解除綁定內置的.click()處理程序。雖然這是一個混亂的黑客,如果你想這樣做,我會讓其他人解釋它。我建議你只是找到一個不同的插件,這意味着以這種方式工作,或重新考慮你在做什麼。

+0

我嘗試這種方式並沒有對輸出產生影響。現在我只看到價值。我添加了我想要的問題代碼 – specked 2010-09-20 19:27:39

+0

我不認爲formatItem適用於這個較新的自動完成插件。任何其他想法?我正在使用jQuery-ui插件 – specked 2010-09-21 12:24:36