我的网站上有一个搜索框。它工作正常,但突然停止工作,当我添加我的网站视频页面。我不断收到一个错误说“未捕获的ReferenceError:搜索没有定义”,似乎我无法找到是什么错误为什么搜索没有定义?
的header.php
<center>
<input type="text" id="searchbar" onkeydown="search()" style="width:60%;font-size:17px;font-weight:bold; height:40px; padding:0px 10px; margin:3px 0px 0px 0px; border-radius:15px;" placeholder="Search for People, Videos, #hashtags and Blogs..." value="<?php echo $search; ?>"/>
</center>
我的页脚页面:footer.php
function search(){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
var str = post = document.getElementById("searchbar").value;
if(str.indexOf("#") == 0){
var result = str.replace('#', '');
var result = result.replace(/ /g, '%20');
window.location.assign("http://www.daparadise.com/testing/daparadise2/search.php?type=hashtag&search=" + result);
}else if(str.indexOf("people named") == 0){
var result = str.replace('people named ', '');
var result = result.replace(/ /g, '%20');
window.location.assign("http://www.daparadise.com/testing/daparadise2/search.php?type=people&search=" + result);
}else if(str.indexOf("videos") == 0){
var result = str.replace('videos ', '');
var result = result.replace(/ /g, '%20');
window.location.assign("http://www.daparadise.com/testing/daparadise2/search.php?type=videos&search=" + result);
}else if(str.indexOf("blogs") == 0){
var result = str.replace('blogs ', '');
var result = result.replace(/ /g, '%20');
window.location.assign("http://www.daparadise.com/testing/daparadise2/search.php?type=blogs&search=" + result);
}else{
var result = result.replace(/ /g, '%20');
window.location.assign("http://www.daparadise.com/testing/daparadise2/search.php?type=web&search=" + result);
}
}
}
关于你的编码风格的一些事情 - 在函数的顶部声明你的变量只有一次;使用===与0比较;不要声明你永远不会使用的变量(在这种情况下变量post);正确使用正则表达式/ \ s /而不是/ /,如果一切都失败,最后会有一个默认情况,就像你的情况一样。 – hex494D49