-1

我在 keyup 事件上调用函数问题是每当我在文本区域输入时它显示重复的结果假设我在文本区域输入 google.com 它应该显示 google.com 的结果它确实但最终结果总是出乎意料goo 或 googl 当我在文本框中快速键入时

<script>
        $(document).ready(function () {
            $('#searchip').keyup(function(){
                $.ajax({

                    type: "GET",
                    url: "whois.php",
                    data: 'ip=' + $('#searchip').val(),
                    success: function(msg){
                        $('#resultip').html(msg);
                    }
                }); // Ajax Call
            }); //event handler
        }); //document.ready
4

1 回答 1

1

使用超时对其进行一点反跳怎么样:

 $(document).ready(function () {
     var timeout, request;
     $('#searchip').keyup(function () {
         clearTimeout(timeout);
         if(request) request.abort();
         timeout = setTimeout(function () {                 
             request = $.ajax({    
                 type: "GET",
                 url: "whois.php",
                 data: 'ip=' + $('#searchip').val(),
                 success: function (msg) {
                     $('#resultip').html(msg);                         
                 }
             }).done(function(){request = null;}); // Ajax Call
         }, 150);
     }); //event handler
 });
于 2013-06-28T08:40:18.483 回答