0

私は Ajax がdiv初めてで、ユーザーが入力フィールドに何かを入力しても何も起こらない場合、このコードはリアルタイムで動的に変更する必要があります。

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

    <!--api to use ajax-->
    <script type="text/javascript" src="sarissa/gr/abiss/js/sarissa/sarissa.js"> </script>
    <title>Ajax Test</title>

    <script>
        function loadXMLDoc() {

            var xmlhttp;
            //parameter
            var suggest = document.getElementById("searchbox").value;

            //using XMLHttpRequest
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            } else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            //when xmlhttp be ready...
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    //the HTML will be modify
                    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                }
            }

            //the file that will receive and retrieve info
            xmlhttp.open("POST","enviar.php",true);

            //header to the retrieved information
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

            //the param will be send
            var sendParam = "fname="+suggest;
            xmlhttp.send(sendParam);
        }

</script>
</head>
<body>
Busca: <input type="text" id="searchbox" />
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onkeypress="loadXMLDoc()">Change Content</button>

</body>
</html>
4

2 に答える 2

1

onkeypressイベントを に変更しonclickます。

ユーザーの入力に合わせてコンテンツを変更したい場合はonkeyup、テキスト入力要素にイベントを追加します。

于 2013-05-24T19:14:35.720 に答える