2012-12-19 28 views
1

我有一個表單當我完成在文本字段中輸入第10個字符時,表單應該自動搜索,但下面的代碼正在搜索每個我在文本字段中輸入的每個字符。 。 。我只想在完成第10個字符後的結果,而不是每個字符的每個字符。 。我已經使用onkeyup事件,我該值設置爲10,但它仍然是尋找每個n每個字符...請你幫我表單搜索Onkeyup事件

<body OnKeyPress="return disableKeyPress(event)"> 
<section id="content" class="container_12 clearfix" data-sort=true> 
    <center><table class='dynamic styled with-prev-next' data-table-tools='{'display':true}' align=center> 
     <script> 
     function disableEnterKey(e) 
     { 
      var key;  
      if(window.event) 
       key = window.event.keyCode; //IE 
      else 
       key = e.which; //firefox  

      return (key != 13); 
     } 

     function showUser(str) 
     { 

      if (str=="") 
      { 
       document.getElementById("txtHint").innerHTML=""; 
       return; 
      } 
      if (window.XMLHttpRequest) 
      {// code for IE7+, Firefox, Chrome, Opera, Safari 
       xmlhttp=new XMLHttpRequest(); 
      } 
      else 
      {// code for IE6, IE5 
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      xmlhttp.onreadystatechange=function() 
      { 
       if (xmlhttp.readyState==4 && xmlhttp.status==200) 
       { 
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
       } 
      } 
      xmlhttp.open("GET","resdb.php?id="+str,true); 
      xmlhttp.send(); 
     } 
     </script> 

     <script type='text/javascript'> 
     //<![CDATA[ 
     $(window).load(function(){ 
      $('#id').keyup(function(){ 
       if(this.value.length ==10) 
      }); 
     });//]]> 

     </script> 



     <form id="form" method="post" name="form" > 
      <tr><td><p align="center"><font size="3"><b>JNTUH - B.Tech IV Year II Semester (R07) Advance Supplementary Results - July 2012</b></font></p></td></tr> 
      <td><p align="center"><b>Last Date for RC/RV : 8th August 2012</b></p></td> 
      <tr><td><p align="center"></b> <input type="text" onkeyup="showUser(this.value)" onKeyPress="return disableEnterKey(event)" data-type="autocomplete" data-source="extras/autocomplete1.php" name="id" id="id" maxlength="10" placeholder="Hall-Ticket Number">&emsp;</p></td></tr> 
     </form> 
    </center> 
</table> 
<center> 
    <div id="txtHint"><b>Results will be displayed here</b></div> 
</center> 
</body> 
+0

看到代碼看起來如此古老,這真是太神奇了。這是從哪裏來的?你想做什麼? – elclanrs

+0

@elclanrs對不起。 。我沒有得到你。 。仍然請你可以幫助我,或者在這個或者任何新的方法中給我建議。 。 Thankyou – Aryan

+1

這是關於'中心'和'B'標籤,它只是讓我想起過去的日子。但作爲提示,您可以將第一塊代碼檢查到密鑰,您可以將其縮減爲一行:'var key = e.which || e.keyCode' – elclanrs

回答

1

這是一個想法,你的代碼工作。這樣做。首先檢查您的文本字段是否超過10個字符。調用fnc()onkeyup

function fnc() 
{ 
    length=document.getElementById("atext").value.length; 
    if(length==10) 
    { 
     callyourajaxfunction() 
    } 


} 
+0

現在沒有結果顯示! – Aryan

+1

@雅利安你有超過10個字符? – polin

+0

不僅10個字符因爲已經限制了文本字段爲10個字符,我只需要輸入第10個字符時就得到結果 – Aryan