控制keyUp jquery事件的行为

时间:2013-06-28 08:35:45

标签: jquery events keyup

我在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

1 个答案:

答案 0 :(得分: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
 });