[英]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.