我正在开发一个项目,该项目需要从地点api返回的结果被过滤掉。我是通过hide()函数来完成的。
$(object).hide();
但是,如果用户使用箭头键滚动查看结果,结果将显示在输入中。
我要做的是禁用该输入框的箭头键导航。
我已尝试使用keydown事件检查,但箭头键导航仍然有效。
$('#search').bind('keydown', function(e) {
var key = (e.keyCode ? e.keyCode : e.which);
if(key == 40 || key == 38) {
e.stopPropagation();
return false;
}
return true;
});
我有什么遗失的东西吗?我也尝试过使用bindFirst插件 - https://github.com/private-face/jquery.bind-first
答案 0 :(得分:0)
试试吧。
保留对禁用功能的参考&准备启用时取消绑定。
例如。
var arrKey = new Array(37, 38, 39, 40);
var disableArrowKeys = function(e) {
if ($.inArray(e.keyCode, arrKey)>=0) {
e.preventDefault();
}
}
$(document).keydown(disableArrowKeys);
// When you are ready to enable, Unbind the function.
$(document).unbind('keydown', disableArrowKeys);
答案 1 :(得分:0)
对于所有遇到同样问题的人,这是我发现的。
绑定事件在google事件绑定之前触发。所以最终解决了我的问题是 -
$('#search').bind('keydown', function (event) {
setTimeout(function() {
// do things here
$('.pac-item').removeClass('.pac-item-selected');
event.stopPropagation();
return false;
}, 2);
});
它现在解决了我的问题。