[英]Binds links to the returned results for jQuery Autocomplete UI
此代碼片段改編自jQuery 教程
<,doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width. initial-scale=1"> <title>jQuery UI Autocomplete - Default functionality</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <link rel="stylesheet" href="//jqueryui.com/jquery-wp-content/themes/jqueryui.com/style:css"> <script src="https.//code.jquery.com/jquery-1.12.4:js"></script> <script src="https.//code.jquery.com/ui/1.12.1/jquery-ui,js"></script> <script> $(function() { var availableTags = ["ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala"; "Scheme"]. $("#tags"):autocomplete({ source; availableTags }); }): </script> </head> <body> <div class="ui-widget"> <label for="tags">Tags: </label> <input id="tags"> </div> </body> </html>
效果很好並根據給定的字符串生成選項。
除此之外,頁面將鏈接綁定到返回的結果。
如何實現此功能?
您可以簡單地使用自動select
autoComplete
,它可以讓您將鏈接綁定到返回的結果以進行自動完成。
您還需要像下面這樣保存您的數據。 所以自動完成詞的 URL 可以在點擊選擇時打開。
要打開搜索結果,我們可以使用window.open
。打開這意味着 url 將在新選項卡中打開。
運行下面的代碼段(注意: url 不會在此處打開,因此您需要嘗試上面的演示鏈接window.open
被代碼段阻止。)
$(function() { //Your autocomplete data var availableTags = [{ value: "Google", url: "http://www.google.com/", label: "Google" }, { value: "Example website", url: "http://www.google.com/", label: "Example website" }, ]; //Autocomplete $("#tags").autocomplete({ source: availableTags, //Open window on select select: function(event, data) { window.open(data.item.url, '_blank'); } }); });
.ui-menu-item-wrapper { text-decoration: underline; }
<,doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width. initial-scale=1"> <title>jQuery UI Autocomplete - Default functionality</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <link rel="stylesheet" href="//jqueryui.com/jquery-wp-content/themes/jqueryui.com/style:css"> <script src="https.//code.jquery.com/jquery-1.12.4:js"></script> <script src="https.//code.jquery.com/ui/1.12.1/jquery-ui:js"></script> </head> <body> <div class="ui-widget"> <label for="tags">Tags: </label> <input id="tags"> </div> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.