2013-04-29 11 views
0

我想在輸入達到7個字符時自動提交表單。我已經嘗試了幾個Java腳本代碼,但它損壞了我的腳本功能。自動提交表單的最大長度, - 爲現有的一個添加一個java腳本代碼

任何一個可以請幫我在這.....

<script type="text/javascript"> 
     var url = "GetCustomerData.php?id="; // The server-side script 
     function handleHttpResponse() { 
     if (http.readyState == 4) { 
       if(http.status==200) { 
       var results=http.responseText; 
       document.getElementById('divCustomerInfo').innerHTML = results; 
       } 
      } 
     } 

     function requestCustomerInfo() {  
      var sId = document.getElementById("txtCustomerId").value; 
      http.open("GET", url + escape(sId), true); 
      http.onreadystatechange = handleHttpResponse; 
      http.send(null); 
     } 
function getHTTPObject() { 
    var xmlhttp; 

    if(window.XMLHttpRequest){ 
    xmlhttp = new XMLHttpRequest(); 
    } 
    else if (window.ActiveXObject){ 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    if (!xmlhttp){ 
     xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
    } 

} 
    return xmlhttp; 


} 
var http = getHTTPObject(); // We create the HTTP Object 
</script> 

<form id="form_home"> 
    <p>Enter customer ID number to retrieve information:</p> 
    <p>Customer ID: <input type="text" maxlength="7" id="txtCustomerId" value="" /></p> 
    <p><input type="submit" value="Submit" onclick="requestCustomerInfo()" /></p> 
</form> 

    <div id="divCustomerInfo"></div> 

回答

0

這裏是將提交表單代碼時的文本框中達到7個字符:

document.getElementById('txtCustomerId').addEventListener('keyup', function(e) { 

    if(this.value.length === 7) { 
     document.getElementById('form_home').submit(); 
    } 
}); 

這裏它的工作演示: http://jsfiddle.net/TuVN2/1/

看着你的標記,我想你想運行requestCustomerInfo(),而不是提交。如果您提交的答覆將永遠不會被處理。要做到這一點,你可以將函數調用移入keyup處理函數:

document.getElementById('txtCustomerId').addEventListener('keyup', function(e) { 

    if(this.value.length === 7) { 
     requestCustomerInfo(); 
    } 

}); 

我也不建議手動滾動你的ajax處理函數。考慮使用像jQuery這樣的庫。

+0

沒有工作。現在我的腳本也沒有工作後添加此.... – 2013-04-29 14:39:14

+0

我編輯我的答案是更明確。這不是解決方案的下降 - 問你是否需要幫助瞭解發生了什麼。 – 2013-04-29 15:00:46

相關問題