我正在使用awesomplete autocomplete plugin,我需要重新定位我在单击任何结果时选择的项目,例如我写了一个Javascript,点击它之后,它必须重定向我想要的任何页面。我请参阅events,但我不知道如何使用它
我的结构
$(document).ready(function() {
});

.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;
}

<input class="awesomplete dropdown-input" list="mylist" id="my-input" />
<datalist id="mylist">
<option data-link="http://www.google.com">Ada</option>
<option data-link="http://www.yahoo.com">Java</option>
<option data-link="http://www.bing.com">JavaScript</option>
<option data-link="http://www.yandex.com">Brainfuck</option>
<option data-link="http://www.php.net">LOLCODE</option>
<option data-link="http://www.asp.net">Node.js</option>
<option data-link="http://www.microsoft.net">Ruby on Rails</option>
</datalist>
<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>
&#13;
答案 0 :(得分:1)
您可以像这样使用 awesomplete-selectcomplete
document.getElementById('my-input').addEventListener('awesomplete-selectcomplete',function(){
alert(this.value);
//TODO HERE
});
.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;
}
<input class="awesomplete dropdown-input" list="mylist" id="my-input" />
<datalist id="mylist">
<option data-link="http://www.google.com">Ada</option>
<option data-link="http://www.yahoo.com">Java</option>
<option data-link="http://www.bing.com">JavaScript</option>
<option data-link="http://www.yandex.com">Brainfuck</option>
<option data-link="http://www.php.net">LOLCODE</option>
<option data-link="http://www.asp.net">Node.js</option>
<option data-link="http://www.microsoft.net">Ruby on Rails</option>
</datalist>
<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>
答案 1 :(得分:1)
获取所选项目的属性没有原生支持。
要绑定事件,请使用所选选择器上的on
功能。
要获取data-link
属性,您需要获取datalist
并找到带有文字值的option
。
Awesomplete是两次触发事件 - 一次在输入字段上,一次在包含div上。我正在通过检查它的兄弟是datalist
来监听div上的事件。完成后,我会使用option
选择器找到所需的:contains
,并传入输入字段值。
我必须将它包装在setTimeout
函数中,因为此时,Awesomplete尚未填充输入字段的value属性。
$(document).ready(function() {
$('.awesomplete').on('awesomplete-select', function(){
var $this = $(this),
$sibling = $this.next();
if($sibling.attr('id') == 'mylist') {
setTimeout(function(){
var val = $this.find('input').val();
var dataLink = $sibling.find('option:contains("' + val + '")').data('link');
console.log(dataLink);
//location.href = dataLink;
}, 500);
}
});
});
.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;
}
<input class="awesomplete dropdown-input" list="mylist" id="my-input" />
<datalist id="mylist">
<option data-link="http://www.google.com">Ada</option>
<option data-link="http://www.yahoo.com">Java</option>
<option data-link="http://www.bing.com">JavaScript</option>
<option data-link="http://www.yandex.com">Brainfuck</option>
<option data-link="http://www.php.net">LOLCODE</option>
<option data-link="http://www.asp.net">Node.js</option>
<option data-link="http://www.microsoft.net">Ruby on Rails</option>
</datalist>
<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>