如何重定向自动完成?

时间:2017-04-04 20:43:13

标签: javascript jquery autocomplete

我正在使用awesomplete autocomplete plugin并且我想在关注myinput时打开自动完成列表但是当我点击任何选项时,我的选择必须重定向任何链接我想要的。我可以这样做吗?

.box{
  width:960px;
}
.awesomplete > ul {
	border-radius: .3em;
	margin: .2em 0 0;
	background: hsla(0,0%,100%,.9);
	background: linear-gradient(to bottom right, white, hsla(0,0%,100%,.8));
	border: 1px solid rgba(0,0,0,.3);
	box-shadow: .05em .2em .6em rgba(0,0,0,.2);
	text-shadow: none;
}

@supports (transform: scale(0)) {
	.awesomplete > ul {
		transition: .3s cubic-bezier(.4,.2,.5,1.4);
		transform-origin: 1.43em -.43em;
	}
	
	.awesomplete > ul[hidden],
	.awesomplete > ul:empty {
		opacity: 0;
		transform: scale(0);
		display: block;
		transition-timing-function: ease;
	}
}

	/* Pointer */
	.awesomplete > ul:before {
		content: "";
		position: absolute;
		top: -.43em;
		left: 1em;
		width: 0; height: 0;
		padding: .4em;
		background: white;
		border: inherit;
		border-right: 0;
		border-bottom: 0;
		-webkit-transform: rotate(45deg);
		transform: rotate(45deg);
	}

	.awesomplete > ul > li {
		position: relative;
		padding: .2em .5em;
		cursor: pointer;
	}
	
	.awesomplete > ul > li:hover {
		background: hsl(200, 40%, 80%);
		color: black;
	}
	
	.awesomplete > ul > li[aria-selected="true"] {
		background: hsl(205, 40%, 40%);
		color: white;
	}
	
		.awesomplete mark {
			background: hsl(65, 100%, 50%);
		}
		
		.awesomplete li:hover mark {
			background: hsl(68, 100%, 41%);
		}
		
		.awesomplete li[aria-selected="true"] mark {
			background: hsl(86, 100%, 21%);
			color: inherit;
		}
<div class="box">
<input class="awesomplete" list="mylist"/>
  <datalist id="mylist">
	<option>Ada</option>
	<option>Java</option>
	<option>JavaScript</option>
	<option>Brainfuck</option>
	<option>LOLCODE</option>
	<option>Node.js</option>
	<option>Ruby on Rails</option>
</datalist>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/awesomplete/1.1.1/awesomplete.min.js"></script>

例如,当我输入Javascript或选择Javascript时,页面必须重定向http://www.blabla.com

2 个答案:

答案 0 :(得分:0)

oninput 事件的回调添加到输入

<input class="awesomplete" oninput="callBack()" list="mylist"/>

然后添加一个脚本函数:

function callBack(){
  window.location.href = "http://stackoverflow.com";
}

答案 1 :(得分:0)

$("#datalist").change(function(){
console.log("working");
});
set id like this for the input.

<input class="awesomplete" list="mylist" id="datalist"/>