Javascript 选择自动完成输入值将转到链接
我希望我的网站加载一个新的页面,这取决于用户从自动完成字段中选择的内容。例如,如果您选择“艺术学校”,则会加载一个包含学校负荷信息的页面,但如果您选择“历史学校”,则会加载另一个页面 到目前为止,我已经完成了自动完成功能,并编写了一个函数,根据您选择的选项转到不同的页面,但实际上它还没有转到页面 控制台中没有任何错误,因此我不确定如何定位代码的哪一部分是错误的 谢谢Javascript 选择自动完成输入值将转到链接,javascript,jquery,html,Javascript,Jquery,Html,我希望我的网站加载一个新的页面,这取决于用户从自动完成字段中选择的内容。例如,如果您选择“艺术学校”,则会加载一个包含学校负荷信息的页面,但如果您选择“历史学校”,则会加载另一个页面 到目前为止,我已经完成了自动完成功能,并编写了一个函数,根据您选择的选项转到不同的页面,但实际上它还没有转到页面 控制台中没有任何错误,因此我不确定如何定位代码的哪一部分是错误的 谢谢 function autocomplete(inp, arr) { var currentFocus; inp.addE
function autocomplete(inp, arr) {
var currentFocus;
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
this.parentNode.appendChild(a);
for (i = 0; i < arr.length; i++) {
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
b = document.createElement("DIV");
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
b.addEventListener("click", function(e) {
inp.value = this.getElementsByTagName("input")[0].value;
closeAllLists();
});
a.appendChild(b);
}
}
});
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
currentFocus++;
addActive(x);
} else if (e.keyCode == 38) { //up
currentFocus--;
addActive(x);
} else if (e.keyCode == 13) {
e.preventDefault();
if (currentFocus > -1) {
if (x) x[currentFocus].click();
}
}
});
function button(text) {
var string = "";
document.getElementById("tags").value = 'Go to your Department';
text = text.toLowerCase();
switch(text){
case 'kent school of architecture':
string = 'architecture';
break;
case 'school of arts':
string = 'arts';
break;
case 'school of english':
string = 'english';
break;
case 'school of european culture and languages':
string = 'ecl';
break;
case 'school of history':
string = 'hist';
break;
case 'school of music and fine art':
string = 'mfa';
break;
default:
string = '';
}
if (string != ''){
window.location.href = "https://"+ string;
}
else{
window.location.reload();
}
};
function addActive(x) {
if (!x) return false;
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
except the one passed as an argument:*/
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
var schools = [ "Kent School of Architecture","School of Arts","School of English","School of European Culture and Languages", "School of History", "School of Music and Fine Art"];
autocomplete(document.getElementById("tags"), schools);
$(document).ready(function() {
$('#tagsautocomplete-list').click( function() {
var text = document.getElementById("tags").value
document.getElementById("tags").value = 'Go to your Department';
button(text);
})
});
功能自动完成(inp、arr){
无功电流聚焦;
inp.addEventListener(“输入”,函数(e){
var a,b,i,val=该值;
closeAllList();
如果(!val){返回false;}
currentFocus=-1;
a=document.createElement(“DIV”);
a、 setAttribute(“id”,this.id+“自动完成列表”);
a、 setAttribute(“类”、“自动完成项”);
this.parentNode.appendChild(a);
对于(i=0;i”+arr[i].substr(0,val.length)+“”;
b、 innerHTML+=arr[i].substr(val.length);
b、 innerHTML+=“”;
b、 addEventListener(“单击”,函数(e){
inp.value=this.getElementsByTagName(“输入”)[0].value;
closeAllList();
});
a、 儿童(b);
}
}
});
inp.addEventListener(“向下键控”,函数(e){
var x=document.getElementById(this.id+“自动完成列表”);
如果(x)x=x.getElementsByTagName(“div”);
如果(e.keyCode==40){
currentFocus++;
addActive(x);
}如果(e.keyCode==38){//up
当前焦点--;
addActive(x);
}否则如果(e.keyCode==13){
e、 预防默认值();
如果(当前焦点>-1){
如果(x)x[currentFocus]。单击();
}
}
});
功能按钮(文本){
var字符串=”;
document.getElementById(“tags”).value='转到您的部门';
text=text.toLowerCase();
开关(文本){
案例“肯特建筑学院”:
字符串='architecture';
打破
“艺术学院”案例:
字符串='arts';
打破
案例“英语学校”:
字符串='english';
打破
案例“欧洲文化和语言学院”:
字符串='ecl';
打破
“历史学院”案例:
字符串='hist';
打破
案例“音乐与美术学院”:
字符串='mfa';
打破
违约:
字符串=“”;
}
如果(字符串!=''){
window.location.href=“https://”+字符串;
}
否则{
window.location.reload();
}
};
函数addActive(x){
如果(!x)返回false;
清除活性(x);
如果(currentFocus>=x.length)currentFocus=0;
如果(currentFocus<0)currentFocus=(x.length-1);
x[currentFocus].classList.add(“自动完成活动”);
}
函数removeActive(x){
对于(变量i=0;i
我的html:
<div class="input-group">
<input name="tags" id="tags" autocomplete="on" class="form-control ui-autocomplete-input form-control-md" type="text" placeholder="Enter your school name...">
<div id="tagsautocomplete-list" class="autocomplete-items"><div><strong>School of Art</strong>s<input type="hidden" value="School of Arts"></div></div>
</div>
艺术学院
谢谢 我是这样做的:
function button(text) {
var string = "";
document.getElementById("tags").value = 'Go to your Department';
text = text.toLowerCase();
switch(text){
case 'kent school of architecture':
string = 'architecture';
break;
case 'school of arts':
string = 'arts';
break;
case 'school of english':
string = 'english';
break;
case 'school of european culture and languages':
string = 'ecl';
break;
case 'school of history':
string = 'hist';
break;
case 'school of music and fine art':
string = 'mfa';
break;
default:
string = '';
}
if (string != ''){
window.location.href = "https://kentunion.co.uk/"+ string;
}
else{
window.location.reload();
}
};
function autocomplete(inp, arr) {
var currentFocus;
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
this.parentNode.appendChild(a);
for (i = 0; i < arr.length; i++) {
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
b = document.createElement("DIV");
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
b.addEventListener("click", function(e) {
inp.value = this.getElementsByTagName("input")[0].value;
closeAllLists();
button(inp.value);
});
a.appendChild(b);
}
}
});
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
currentFocus++;
addActive(x);
} else if (e.keyCode == 38) { //up
currentFocus--;
addActive(x);
} else if (e.keyCode == 13) {
e.preventDefault();
if (currentFocus > -1) {
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
if (!x) return false;
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
var schools = [ "Kent School of Architecture","School of Arts","School of English","School of European Culture and Languages", "School of History", "School of Music and Fine Art"];
autocomplete(document.getElementById("tags"), schools);
功能按钮(文本){
var字符串=”;
document.getElementById(“tags”).value='转到您的部门';
text=text.toLowerCase();
开关(文本){
案例“肯特建筑学院”:
字符串='architecture';
打破
“艺术学院”案例:
字符串='arts';
打破
案例“英语学校”:
字符串='english';
打破
案例“欧洲文化和语言学院”:
字符串='ecl';
打破
“历史学院”案例:
字符串='hist';
打破
案例“音乐与美术学院”:
字符串='mfa';
打破
违约:
字符串=“”;
}
如果(字符串!=''){
window.location.href=”https://kentunion.co.uk/“+字符串;
}
否则{
window.location.reload();
}
};
功能自动完成(inp、arr){
无功电流聚焦;
inp.addEventListener(“输入”,函数(e){
var a,b,i,val=该值;
closeAllList();
如果(!val){返回false;}
currentFocus=-1;
a=document.createElement(“DIV”);
a、 setAttribute(“id”,this.id+“自动完成列表”);
a、 setAttribute(“类”、“自动完成项”);
this.parentNode.appendChild(a);
对于(i=0;i”+arr[i].substr(0,val.length)+“”;
b、 innerHTML+=arr[i].substr(val.length);
b、 innerHTML+=“”;
b、 阿登特利斯