javascript
  • jquery
  • ajax
  • 2013-05-13 105 views 0 likes 
    0

    无法解决我在这里做错了什么。如果搜索字段中没有任何内容或网站的任何区域被点击,我希望自动完成div关闭,类似于onblur。相反,div保持存在。这是我的代码:关闭自动完成

    $(document).ready(function(){ 
    
    $(".search").keyup(function() 
    { 
    var searchbox = $(this).val(); 
    var dataString = 'searchword='+ searchbox; 
    
    if(searchbox=='') 
    { 
    } 
    else 
    { 
    
    $.ajax({ 
    type: "POST", 
    url: "search.php", 
    data: dataString, 
    cache: false, 
    success: function(html) 
    { 
    
    $("#display").html(html).show(); 
    
    
        } 
    
    }); 
    }return false;  
    
    }); 
    }); 
    

    “Display”是我的自动完成div。在其他地方cliked时隐藏display

    see demonstration

    var handler = function(event){ // to hide "#display" on elsewhere click 
    if($(event.target).not("#display *") || $(".search").val().length != 0) return;  
        $("#display").hide(); 
    } 
    
    $(document).on("click", handler); 
    

    以上处理器 -

    +1

    为什么不试试jQuery的自动完成HTTP://jqueryui.com/autocomplete ???? – Shin 2013-05-13 04:10:32

    +0

    尝试alert(html); ,得到任何结果? – Shin 2013-05-13 04:15:25

    回答

    0

    试试这个。如果点击里面display它不会隐藏。

    而且

    $(".search").keyup(function(){ 
        var searchbox = $(this).val(); 
        if(searchbox.length === 0) // if field is blank hide div again. 
          $("#result").hide(); 
    .... // remaining code ---- 
    
    相关问题