亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

綁定指向 jQuery Autocomplete UI 返回結果的鏈接

綁定指向 jQuery Autocomplete UI 返回結果的鏈接

胡子哥哥 2022-12-02 17:09:43
此代碼片段改編自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>效果很好,并根據給定的字符串生成選項。除此之外,一個頁面將鏈接綁定到返回的結果。我如何實現此功能?
查看完整描述

1 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

您可以簡單地使用autoComplete select功能,它將鏈接綁定到返回的結果以進行自動完成。

您還需要像下面這樣保存您的數據。所以自動補全詞的URL可以在點擊選擇的時候打開。

要打開搜索結果,我們可以使用window.open這意味著 url 將在新選項卡中打開。

工作演示: https ://jsfiddle.net/09dtrk7L/2/

運行下面的代碼片段(注意:網址不會在此處打開,因此您需要嘗試上面的演示鏈接。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>


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


查看完整回答
反對 回復 2022-12-02
  • 1 回答
  • 0 關注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號