我的網站上有一個搜索框。它工作正常,但突然停止工作,當我添加我的網站視頻頁面。我不斷收到一個錯誤說「未捕獲的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