1、默认页面
document.getElementById("myInput").addEventListener("keydown", function(event) {
// 按下Enter回车键触发时间
if (event.code === "Enter") {
event.preventDefault(); //阻止默认行为,比如提交表单等操作
window.location.href = '/search.html?s=' + $("#myInput").val()
}
});
2、搜索页面
const url = new URL('https://www.example.com/?s=25');
const params = new URLSearchParams(url.search);
// 通过 get() 方法获取指定参数的值
const value = params.get('s');
console.log(`输出结果: ${value}`); //输出结果: 25
console.log(`${value}`)
//从接口获取数据
$.ajax({
url: '/search.php?name=' + value,
type: 'POST',
data: {},
success: function(res) {
if(res && res.code == 10000){
console.log('获取数据!')
}else{
console.log('获取失败!')
}
},
error: function() {
console.log('请求失败输出');
}
});