1

我有一个带有四个输入字段(联系人 ID、电话号码、姓名、称呼)的简单表单。如果我输入“联系人 ID”并按 Enter 按钮,该字段的其余部分将自动填充从数据库中检索。现在我想在表单中添加一个“提交”按钮,以便通过单击该按钮将这些信息保存到另一个数据库表中。我尝试添加按钮,但自动填充功能不再起作用。有人可以帮忙吗?在此先感谢 :) 这是我的 index.html 文件:

<html> 
    <body> 
        <script language="javascript" type="text/javascript">

    function ajaxFunction(e){
    var e=e || window.event;
    var keycode=e.which || e.keyCode;
    if(keycode==13 || (e.target||e.srcElement).value==''){ 
    var http;  // The variable that makes Ajax possible! 

    try{ 
        // Opera 8.0+, Firefox, Safari 
        http = new XMLHttpRequest(); 
    } catch (e){ 
        // Internet Explorer Browsers 
        try{ 
            http = new ActiveXObject("Msxml2.XMLHTTP"); 
        } catch (e) { 
            try{ 
                http = new ActiveXObject("Microsoft.XMLHTTP"); 
            } catch (e){ 
                // Something went wrong 
                alert("Your browser broke!"); 
                return false; 
            } 
        }
    }

    var url = "getagentids.php?param=";
                var idValue = document.getElementById("agid").value;
                var myRandom = parseInt(Math.random()*99999999);  // cache buster
                http.open("GET", "getagentids.php?param=" + escape(idValue) + "&rand=" + myRandom, true);
                http.onreadystatechange = handleHttpResponse;
                http.send(null);
         function handleHttpResponse() {
                    if (http.readyState == 4) {
                        results = http.responseText.split(",");
                        document.getElementById('agfn').value = results[0];
                        document.getElementById('agsal').value = results[1];
                        document.getElementById('agtel').value = results[2];
                        document.getElementById('agid').value = results[3];
                    }
                } 
    }
}

</script> 

       <form>
            <table>
                <tr>
                    <td>Contact ID:</td>
                    <td><input id="agid" type="text"
                               name="contactid" onkeyup="ajaxFunction(event)"></td>
                </tr>
                <tr>
                    <td>Tel Number:</td>
                    <td><input id="agtel" type="text"
                               name="contacttel"></td>
                </tr>
                <tr>
                    <td>Name:</td>
                    <td><input id="agfn" type="text"
                               name="contactfullname"></td>
                </tr>
                <tr>
                    <td>Salutation:</td>
                    <td><input id="agsal" type="text"
                               name="contactsalutation"></td>
                </tr>
                <tr>
                    <td><input type="reset" value="Clear"></td>
                    <td></td>
                </tr>
            </table>
        </form>

    </body> 
</html>  
4

1 回答 1

1

如果您想在填充表单添加提交按钮,您可以通过 usinginnerHTML

...
if (http.readyState == 4) {
   results = http.responseText.split(",");
   document.getElementById('agfn').value = results[0];
   document.getElementById('agsal').value = results[1];
   document.getElementById('agtel').value = results[2];
   document.getElementById('agid').value = results[3];
   document.getElementById('buttons').innerHTML = "<input type=\"submit\" name=\"submit\" value=\"Submit\"/>";  // add Submit button by innerHTML
}
...

并在您的表格中添加一个<div id="buttons">到您要添加submit按钮的位置

<tr>
    <td><div id="buttons"></div><input type="reset" value="Clear"></td>
    <td></td>
</tr>
于 2012-11-13T17:12:30.593 回答