Javascript 无法停止页面重新加载
使用Javascript 无法停止页面重新加载,javascript,jquery,ajax,Javascript,Jquery,Ajax,使用events.peventDefault()和events.stopPropagation(),无法阻止页面重新加载 bindSearch: function() { var val = null; var input = null; config.bindThis('chatSearchFriends', 'body', 'searchBar', function(value, events) { input = $(value);
events.peventDefault()
和events.stopPropagation()
,无法阻止页面重新加载
bindSearch: function() {
var val = null;
var input = null;
config.bindThis('chatSearchFriends', 'body', 'searchBar', function(value, events) {
input = $(value);
val = $(input).val();
timer = setTimeout(doSearch, 1000);
config.showThis('clearSearch');
var keycode = (events.keyCode ? events.keyCode : events.which);
if (keycode == '13' && !events.shiftKey) {
//prevent page reload
events.stopPropagation();
events.preventDefault();
console.debug('enter');
}
});
config.bindThis('chatClearSearch', 'body', 'clearSearch', function(value, events) {
//clear search text
});
function doSearch() {
//fetch matched search
}
},
在上面的代码中,我将搜索框绑定到键控事件(chatSearchFriends)
当用户键入时,如果他们按enter键,页面不应重新加载,这是我关心的问题
无论用户是否按enter键,ajax都会在1秒后获取匹配的数据。这是通过此行完成的:timer=setTimeout(doSearch,1000)代码>
所以按enter键只会停止页面重新加载。我不确定为什么上面的话不能阻止它
这是:
bindThis: function(eventKey, bindTo, selectorKey, callback, bindOnceOnly = false) {
this.unBindThis(bindTo, eventKey);
if (bindOnceOnly) {
this.getSelectorDOM(bindTo).one(this.events[eventKey], this.selectors[selectorKey], function(e) {
callback(this, e);
});
return;
}
this.getSelectorDOM(bindTo).on(this.events[eventKey], this.selectors[selectorKey], function(e) {
callback(this, e);
});
},
在上面的代码中,我在keyup事件(chatSearchFriends)上绑定searchbox
您不能停止正在按向上键的键的传播。事件已经发生了。它必须是按下键或按键
在上面的代码中,我在keyup事件(chatSearchFriends)上绑定searchbox
您不能停止正在按向上键的键的传播。事件已经发生了。它必须是按下键或按键 在正文中的第一个event.preventDefault()
中,您拼写了它event.peventDefault()
。在他的代码中拼写正确。在将另一个函数绑定到同一个函数之前,请尝试解除绑定object@OkiErieRinaldi,隐藏函数(config.bindThis)会这样做。不知道什么是bindThis
,没有帮助。在正文中的第一个event.preventDefault()
中,您拼写了它event.peventDefault()
。在他的代码中拼写正确。在将另一个函数绑定到同一个函数之前,请尝试解除绑定object@OkiErieRinaldi,隐藏函数(config.bindThis)会这样做。不知道什么是bindThis
,没有帮助。