Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法停止页面重新加载_Javascript_Jquery_Ajax - Fatal编程技术网

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
,没有帮助。