自动提交表单上的最大长度,-为现有的一个添加java脚本代码

auto Submit form on max length, - add a java script code for existing one

本文关键字:一个 脚本 java 添加 代码 表单 提交      更新时间:2023-09-26

我试图在输入达到7个字符时自动提交表单。我已经尝试了一些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>

下面的代码将在文本框达到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之类的库。

相关文章: