2012-03-07 56 views
0

我是jquery &的新手,並且正在嘗試學習它們。按照我的預期,以下方法可行,除非有人輸入東西並點擊ENTER。然後文本框變爲空白。我如何壓制這一點?我希望它能觸發現有事件之一 - 選擇或更改。謝謝,無法捕捉或禁止自動完成中的ENTER按鈕

<!doctype html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <title>Demo</title> 
    <style> 
     a.test { font-weight: bold; } 
    </style> 
    </head> 
    <body> 
    <link rel="stylesheet" type="text/css" href="css/ui-lightness/jquery-ui-1.8.18.custom.css"> 
    <link rel="stylesheet" type="text/css" href="css/autocomplete.css"> 
    <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script> 
    <script type="text/javascript" src="js/jquery-ui-1.8.18.custom.min.js"></script> 
    <script> 
     $(document).ready(function(){ 

      //attach autocomplete 
      $("#suggest4").autocomplete({ 

        //define callback to format results 
        source: function(req, add){ 

          //pass request to server 
          $.getJSON("friends.php?callback=?", req, function(data) { 

            //create array for response objects 
            var suggestions = []; 

            //process response 
            $.each(data, function(i, val){ 
            suggestions.push(val.name); 
          }); 

          //pass array to callback 
          add(suggestions); 
        }); 
      }, 

      //define select handler 
      select: function(e, ui) { 
       $("#spill").html("change "+ui.item.value+" "+e.type); 
      }, 

      //define select handler 

      change: function(e) { 
       $("#spill").html("change "+$(this).val()+e.type); 
      } 

      }); 
     }); 
    </script> 
    <div id="content"> 
     <form> 
     <p> 
      <label>Multiple Birds (local):</label> 
      <input type="text" id="suggest4"></input> 
     </p> 
     </form> 
    </div> 
    <div id="spill"> 
    </div> 
    </body> 
</html> 

回答

0

您需要綁定到輸入按鍵事件,尋找進入,然後做你想要什麼:

$('#suggest4').keypress(function(e) { 
    //catch only enter 
    if(e.keyCode == 13) { 
     e.preventDefault(); 
     //put your stuff in here 
    } 
    }); 
相關問題