Javascript 快速按下回车键

Javascript 快速按下回车键,javascript,php,Javascript,Php,我正在从大约5000条记录中搜索。搜索按钮工作正常。但当我按回车键太快时。它不会把结果带回来。我已经试过stckoverflow.com上的代码了,但它对我不起作用 我的搜索文本框是 <input name="search_value" id="country_name" onkeyup="Javascript: if (event.keyCode == 13) return false; searchFilter();"

我正在从大约5000条记录中搜索。搜索按钮工作正常。但当我按回车键太快时。它不会把结果带回来。我已经试过stckoverflow.com上的代码了,但它对我不起作用

我的搜索文本框是

<input name="search_value" id="country_name"   onkeyup="Javascript: if (event.keyCode == 13) return false; searchFilter();"  />
HTML



它不应该返回结果,因为您从未启动
searchFilter
,如果使用enter,您会看到
if(event.keyCode==13)return false

keycode13==ENTER,这意味着在
searchFilter()之前返回false被调用。
如果(event.keyCode==13)返回false,则删除
if

您需要像这样使用“承诺超时”:

var searchTimer  = null;
var searchInput  = $('#country_name');
var searchResult = $('.contentText');

searchInput.on('keyup', function() {
    clearTimeout(searchTimer);
    searchTimer = setTimeout(function() {
        var val = $.trim(searchInput.val());
        if (val) {
            $.ajax({
                url: 'searchorder.php?search_value=' + val,
                cache: false, // deny cache GET requests
                success: function(data) {
                    searchResult.html(data);
                }
            });
        }
    }, 300);
});
演示

好的,这是你的解决方案

在发出新请求时中止以前的任何请求,以免它们充斥服务器

通过等待用户停止键入来降低请求的速度

按钮可以工作,所以我们需要做的就是绑定keyup并更改输入字段以触发该按钮,而不是复制代码。我们使用jQuery来实现这一点,而不是使用
onkeyup=”“

为了确保我们没有发出不必要的请求,在按下enter键时返回false是正确的,但是我们使用了
.preventDefault()

<input name="search_value" id="country_name"  />

使用
{}
按钮正确格式化代码。用户JavaScript可在按下提交按钮后禁用该按钮。这将防止双击打乱您的通话。@DurbnPoisin这是个好主意,但您需要检查输入字段是否没有更改,否则您只能尝试一次。按enter键也会中止上一个请求。仅供参考,您不需要在
onXXX
属性中使用
javascript:
前缀。它用于将脚本放入通常包含URL的属性中。但是,
onXXX
不包含URL,它包含一个脚本,所以您不需要告诉它这是一个脚本。对不起,这是我第一次在这里提问。但是我如何中止以前的值?它会在每个键上搜索。为什么还要再次搜索相同的请求?Enter不会更改字段中的文本。在搜索之前返回false看起来是正确的。它可能看起来是正确的,但对于这种情况,这就是问题的原因。如果我删除“if(event.keyCode==13)return false;”那么,当按下enter键时,我该如何检查?你能在JSFIDLE中演示一下吗?@POPLACES抱歉,我无法解释这一点。我将应用此解决方案。谢谢你回答我的问题
var searchTimer  = null;
var searchInput  = $('#country_name');
var searchResult = $('.contentText');

searchInput.on('keyup', function() {
    clearTimeout(searchTimer);
    searchTimer = setTimeout(function() {
        var val = $.trim(searchInput.val());
        if (val) {
            $.ajax({
                url: 'searchorder.php?search_value=' + val,
                cache: false, // deny cache GET requests
                success: function(data) {
                    searchResult.html(data);
                }
            });
        }
    }, 300);
});
<input name="search_value" id="country_name"  />
var searchFilterXhr;
var searchTimeout;

$(function(){
    // bind keyup (and change) to trigger the button that causes the search
    $('#country_name').on('keyup change', function(e){
        // prevent any default action, like submitting a form on enter.
        e.preventDefault();
        // if the text field has changed, only then do we search
        if ($(this).data('last-value') != $(this).val()) $('#button').trigger('click');
        // remember the previous value so that we can check it's changed
        $(this).data('last-value', $(this).val());

    });
});

function searchFilter(){
    //alert("");
    $("#loader").show();
    var search_variable = $("#search_variable").val();
    var search_value = $("#country_name").val();
    //alert(search_value);
    if (searchFilterXhr) searchFilterXhr.abort();
    clearTimeout(searchTimeout);
    searchTimeout = setTimeout(function(){
        searchFilterXhr = $.ajax({
            url: 'searchorder.php?act=save&search_variable=' + search_variable + '&search_value=' + search_value,
            success: function(data) {
                //alert(data);
                $("#loader").hide();
                $(".contentText").html(data);
                // $("#feedbackcommon"+act_id).show();
            }
        });
    }, 500);
}