2010-10-18 19 views
0

我已經建立了自動完成輸入框(但與foucsout問題)我已經建立了自動完成輸入框(但與foucsout問題)

關注此: http://www.youtube.com/watch?v=IxlXWfJsSeM

演示: http://www.faressoft.org/autocomplete/

我的HTML代碼:

我的jQuery代碼:

$(".autocomplete").focusout(function() { 
$(".autoCompleteList").css("display","none"); 
}); 

結果應該是像計算器


從鏈路增加實際代碼的標籤輸入框。 --patrick DW

$(document).ready(function(){ 
    $(".autocomplete").attr("value",""); 
    $(".autocomplete").keyup(function() { 
     $(".autoCompleteList").css("display","none"); 
     if ($(this).attr("value")!="") { 
      $(".autoCompleteList").width($(this).width()+3); 
      $(".autoCompleteList").css("display","block"); 
      var Value = $(this).attr("value"); 
      $.ajax({ 
      type: "GET", 
      url: "Tags.php", 
      data: "country=" + Value, 
      dataType: "html", 
      success: function(data) { 
       if (data!="") { 
        $(".autoCompleteList").html(data); 
        var re = new RegExp("(" + Value + ")", "gi"); 
        $('.autoCompleteList').html($('.autoCompleteList').html().replace(re, '<span>$1</span>')); 
        $(".autoCompleteList li").click(function() { 
         $(".autocomplete").attr("value", $(this).text()); 
        }); 
       } else { 
        $(".autoCompleteList").css("display","none"); 
       } 
      } 
      }); 
     } 
    }); 
    $(".autocomplete").focusout(function() { 
     //$(".autoCompleteList").css("display","none"); Watch the video. I can't choose the country. 
    });   
}); 
+0

的可能重複的[問題的事件的內容(jQuery的)](HTTP ://stackoverflow.com/questions/3954857/problem-with-focusout-jquery) – user113716 2010-10-18 17:18:23

+1

@patrick德國之聲:我刪除了 「問題與事件的內容(jQuery的)」 – faressoft 2010-10-18 17:22:03

+0

faressoft - 作爲** @九月Ø月**回答,代碼在你的問題被註釋掉。當我使用未註釋的代碼運行頁面時,它可以工作。我編輯了問題以包含鏈接中的代碼。 – user113716 2010-10-18 17:35:03

回答

4

好,我加入$(".autoCompleteList").hide();線到點擊事件處理程序,並改寫了代碼一點點:

$(function(){ // shorthand for doc.ready  
    (function(){  
     var $input = $(".autocomplete"), // caching 
      $list = $(".autoCompleteList"); 
     $input.attr("value","").keyup(function() { 
      $list.hide(); 
      if ($(this).attr("value")!=="") { 
      $list.width($(this).width()+3).show();  
      var val = $(this).attr("value"); // dont use value as varaiable name 
      $.ajax({ 
      type: "GET", 
      url: "Tags.php", 
      data: "country=" + val, 
      dataType: "html", 
      success: function(data) { 
      if (data!=="") { 
       //replacing data 
       var re = new RegExp("(" + val + ")", "gi"); 
       data = data.replace(re, '<span>$1</span>'); 
       $list.html(data); 

       // binding click 
        $(".autoCompleteList li").bind('click', function() { 
        $(".autocomplete").attr("value", $(this).text()); 
        $(".autoCompleteList").hide(); 
        }); 

      } else { 
       $list.hide(); 
      } 
      } 
      }); 
     } 
    }); 
    })(); // self executing 
}); 
+0

觀看視頻。因爲我不能選擇國家。 – faressoft 2010-10-18 18:41:13

+0

http://www.youtube.com/watch?v=IxlXWfJsSeM – faressoft 2010-10-18 18:44:47

+0

非常好。但是當我點擊頁面時,列表不會隱藏。 – faressoft 2010-10-18 21:33:55