在我的網站上,我有能力讓用戶按使用搜索,輸入,在搜索輸入字段中鍵入了一個字符串。下面是一些jQuery的代碼來說明這一點:重要事件在Google Chrome中無法正常工作
$(document).ready(function() {
$('#search-input').on('keydown keyup keypress', function (event) {
event = event || windows.event;
if (event.keyCode == 13 || event.which == 13) {
$("#search-button").click();
}
});
$("#search-button").click(function() {
var theUrl = "/search.aspx?search=" + document.getElementById('search-input').value;
window.location = theUrl;
});
}
在我的母版頁,我有以下代碼:
<input id="search-input" name="search" type="text" placeholder="Search..." />
<button id="search-button" type="button"></button>
雖然這在Firefox和IE9/IE8,它不會在Chrome中工作(除非在調試模式下令人驚訝)。請幫助我在所有這些瀏覽器中正常工作。
謝謝你的回答。你的代碼是真正的跨瀏覽器,並有助於解決我的問題。只有在現場按下回車鍵,用戶纔會被重定向。 – arthur 2013-02-22 15:51:45
呃!我真的沒有想過,如果有條件的話,忘記了nr.13的關鍵,所以當然沒關係!使用jQuery,只需堅持基礎知識通常是最好的。 – adeneo 2013-02-22 15:57:39