1 回答

TA貢獻1830條經驗 獲得超9個贊
您可以簡單地使用autoComplete
select
功能,它將鏈接綁定到返回的結果以進行自動完成。
您還需要像下面這樣保存您的數據。所以自動補全詞的URL可以在點擊選擇的時候打開。
要打開搜索結果,我們可以使用window.open
這意味著 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>
<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>
添加回答
舉報