gpt4 book ai didi

javascript - 如何添加搜索建议的链接?

转载 作者:行者123 更新时间:2023-11-28 03:09:47 25 4
gpt4 key购买 nike

我是一个相当新的 js 编码新手,而且我对 html 的自学知识只有简短的知识。任何帮助将不胜感激,谢谢。

这是具有自动完成功能的搜索框的 JavaScript。我问如何为每个变量添加链接。

<script>
$(document).ready(function(){
$('input.autocomplete').autocomplete({
data: {
"Single Seats": null,
"Multiple Seats": null,
"Beds": null,
"Entertainment": null,
"Tables": null,
"Storage": null,
"Sets": null,
"Other": null
}
});
});
</script>

最佳答案

检查代码片段,这将重定向到附加到自动完成数组的链接。这里的自动完成数组包含带有值和 url 的对象,页面将根据所选值重定向到适当的 url。为了测试,所有值都设置为 google。 :)

var $ = jQuery;
$( function() {
var availableTags = [
{value:"ActionScript", link:'https://google.com'},
{value:"AppleScript",link:'https://google.com'},
{value:"Asp",link:'https://google.com'},
{value:"BASIC",link:'https://google.com'},
{value:"C",link:'https://google.com'},
{value:"C++",link:'https://google.com'},
{value:"Clojure",link:'https://google.com'},
{value:"COBOL",link:'https://google.com'},
{value:"ColdFusion",link:'https://google.com'},
{value:"Erlang",link:'https://google.com'},
{value:"Fortran",link:'https://google.com'},
{value:"Groovy",link:'https://google.com'},
{value:"Haskell",link:'https://google.com'},
{value:"Java",link:'https://google.com'},
{value:"JavaScript",link:'https://google.com'},
{value:"Lisp",link:'https://google.com'},

];
$( "#tags" ).autocomplete({
source: availableTags,
select: function( event, ui ) {
console.log(ui.item.link);
window.location.replace(ui.item.link)
}
});
} );
<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="/resources/demos/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>

</script>
</head>
<body>

<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>


</body>
</html>

关于javascript - 如何添加搜索建议的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60239889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com