我有一個搜索表單,我想用我的網站在一個框中輸入文本,並使用單選按鈕選擇搜索引擎(網站搜索或我的圖書館目錄)。Javascript搜索表單代碼
此代碼在Firefox和Chrome,但不是IE瀏覽器和Safari瀏覽器的工作原理:
<script type="text/javascript">
function dosearch() {
var sf=document.searchform;
var submitto = sf.sengines.value + escape(sf.searchterms.value);
window.location.href = submitto;
return false;
}
</script>
<form name="searchform" onSubmit="return dosearch();">
<input type="text" name="searchterms" size="20">
<input type="submit" name="SearchSubmit" value="Search">
<br />
<input type="radio" name="sengines" checked value="http://chelmsford.mvlc.org/search/keyword/">Catalog
<input type="radio" name="sengines" value="http://chelmsfordlibrary.org/?s=">Site
</form>
而且我也有這樣的代碼,這在IE工作,但不能在Firefox:
<script>
function myFunction() {
var engine = document.forms[0];
var txt = "";
var term = escape(document.radiosearch.searchterms.value)
var i;
for (i = 0; i < engine.length; i++) {
if (engine[i].checked) {
txt = txt + engine[i].value;
}
}
window.location.href = txt + term;
}
</script>
<form name="radiosearch" onSubmit="return myFunction();">
<input type="text" name="searchterms" size="20">
<input type="submit" value="Search">
<br />
<input type="radio" name="engine" value="http://chelmsford.mvlc.org/search/keyword/" checked>Catalog</input>
<input type="radio" name="engine" value="http://chelmsfordlibrary.org/?s=">Site</input>
</form>
我怎樣才能合併或更正這些,所以一段代碼可以在所有瀏覽器中工作?
你看到在IE或Safari會爲您提供一個線索在調試控制檯中的任何錯誤是怎麼回事錯在那裏? – jfriend00
此外,'escape()'已被棄用 - 你可能想使用'encodeURIComponent()',或者你可能只是做'window.location.href = encodeURI(text + term)'而不是。 – jfriend00
謝謝jfriend00--不幸的是,調試器沒有提供任何幫助。 IE中發生的事情是「未定義」,然後我輸入的任何搜索詞都會附加到當前URL的末尾,而不是交換搜索引擎字符串和window.location的搜索詞。我會繼續工作 - 感謝您的建議。 – herzogbr