想实现类似于淘宝搜索跳转到相对应的网页,通过获取input输入框中的内容后,在js方法中进行匹配后通过链接,跳转到新网页。具体实现代码实在写不出来,想寻求帮助。
不明白js方法中进行匹配后通过链接是什么操作?
下面这样?通过输入关键字得到对应URL后打开?
<input type="text" id="t" /><input type="button" value="搜索" onclick="search()" />
<script>
function search() {
var t = document.querySelector('#t'),
url = 'https://ask.csdn.net/'//默认跳转到问答
;
switch (t.value) {
case '网易': url = 'https://www.163.com'; break;
case '百度': url = 'https://www.baidu.com'; break;
//更多。。。
}
window.open(url)
}
</script>
有帮助请【采纳该答案】,谢谢~~有其他问题可以继续交流~
新窗口用 window.open('url','_blank'),url 就用你的新链接地址
当前窗口就用 location.href = url
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
a {
margin: 0 12px;
}
</style>
</head>
<body>
<input type="text" id="val" value="44">
</body>
<script>
let value=document.getElementById('val').value;
console.log(value);
</script>
</html>
原生js获取input的值:document.getElementById("input_id").value,jquery获取值:$('#input_id').val()