[英]jquery autocomplete .When you have the autocomplete and press 'enter',i do not want to search ,actually it will do search
輸入自動完成功能,您按“ enter”鍵,我會搜索內容。但是當您具有自動完成功能並按“ enter”時,我不想搜索,實際上,當您按“ enter”時,它將進行搜索,因為它將捕獲enter事件並執行搜索功能。
這是第一個功能:自動完成代碼:
$('input[name=fieldInputName]').autocomplete(names, {
max: max,
minChars: minChars ,
width: 149,
scrollHeight: 500,
matchContains: true,
autoFill: false,
sortable: true,
matchSubset :false,
formatItem: function(row, i, max) {
return row[returnSearchkey];
},
formatMatch: function(row, i, max) {
return row[returnSearchkey] ;
},
formatResult: function(row) {
return row[returnSearchkey];
}
}).result(function(event, row, formatted) {
});
這是第二個功能:將搜索的pressenter代碼:
function pressEnter(e){
e = e || event;
if (e.keyCode == 13) {
submitSearch()();
}
}
輸入的HTML:
<input onkeyup="pressEnter(event);"type='text' name='createdBy' />
當您觸發自動完成並通過按“ enter”鍵選擇一個項目時,它將執行自動完成並回填所選的值,但是它還會調用第二個函數“ pressEnter(e)”。實際上,我想在您觸發了自動完成功能,但不執行第二個功能(按Enter)。
DOM中的事件從樹中的某個位置開始,一直到元素(捕獲階段),然后它們冒泡到DOM(冒泡階段)。
您可以捕獲該事件並阻止其傳播。 然后將不再進行擴展,也不會調用附加到例如包裝元素的其他事件處理程序(如您的第二個函數)。
在自動完成的事件處理程序中,應在事件上調用stopPropagation()
。 但這在所有瀏覽器中均不起作用。 在IE9之前的IE中,該方法稱為cancelBubble(true)
。
為了防止瀏覽器出現默認行為,您可以執行event.preventDefault()
您可以通過搜索“事件取消”或“事件傳播”來了解更多信息,因為這是JavaScript中的棘手主題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.