绑定指向 jQuery Autocomplete UI 返回结果的链接

此代码片段改编自jQuery 教程


<!doctype html>

<html>


<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>

效果很好,并根据给定的字符串生成选项。

http://img4.mukewang.com/6389c2850001723301480088.jpg

除此之外,一个页面将链接绑定到返回的结果。

http://img1.mukewang.com/6389c2900001af0910220335.jpg

我如何实现此功能?



胡子哥哥
浏览 171回答 1
1回答

慕标琳琳

您可以简单地使用autoComplete&nbsp;select功能,它将链接绑定到返回的结果以进行自动完成。您还需要像下面这样保存您的数据。所以自动补全词的URL可以在点击选择的时候打开。要打开搜索结果,我们可以使用window.open这意味着 url 将在新选项卡中打开。工作演示:&nbsp;https ://jsfiddle.net/09dtrk7L/2/运行下面的代码片段(注意:网址不会在此处打开,因此您需要尝试上面的演示链接。window.open被代码片段阻止了。)$(function() {&nbsp; //Your autocomplete data&nbsp; var availableTags = [{&nbsp; &nbsp; &nbsp; value: "Google",&nbsp; &nbsp; &nbsp; url: "http://www.google.com/",&nbsp; &nbsp; &nbsp; label: "Google"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; value: "Example website",&nbsp; &nbsp; &nbsp; url: "http://www.google.com/",&nbsp; &nbsp; &nbsp; label: "Example website"&nbsp; &nbsp; },&nbsp; ];&nbsp; //Autocomplete&nbsp; $("#tags").autocomplete({&nbsp; &nbsp; source: availableTags,&nbsp; &nbsp; //Open window on select&nbsp; &nbsp; select: function(event, data) {&nbsp; &nbsp; &nbsp; window.open(data.item.url, '_blank');&nbsp; &nbsp; }&nbsp; });});.ui-menu-item-wrapper {&nbsp; text-decoration: underline;}<!doctype html><html><head>&nbsp; <meta charset="utf-8">&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1">&nbsp; <title>jQuery UI Autocomplete - Default functionality</title>&nbsp; <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">&nbsp; <link rel="stylesheet" href="//jqueryui.com/jquery-wp-content/themes/jqueryui.com/style.css">&nbsp; <script src="https://code.jquery.com/jquery-1.12.4.js"></script>&nbsp; <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script></head><body>&nbsp; <div class="ui-widget"> <label for="tags">Tags: </label> <input id="tags"> </div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript