我有一個簡單的搜索功能是行爲不端。搜索框位於我們不想在搜索時提交的表單內,因此我在下面進行了設置。jQuery preventDefault沒有行爲
$(function() {
jQuery.fn.onEnter = function(callback) {
this.keyup(function(e) {
if(e.keyCode == 13) {
e.preventDefault();
//e.stopPropagation();
if (typeof callback == 'function')
callback.apply(this);
}
});
return this;
}
$('#txtSearch').onEnter(function() {
search();
return false;
});
$('#search_btn').click(function() {
search();
return false;
});
function search() {
alert('searching');
}
});
e.preventDefault()行不起作用,所以搜索功能會像表單提交一樣觸發。我已經嘗試過e.preventDefault()
和e.stopPropagation()
,但沒有運氣。
回車鍵表單內將觸發表單的提交功能 - 這樣處理它在那裏 –
你停止瀏覽器的默認keyup事件不能提交事件。 –
我建議你使用'return false'。 – Rajnish