PHP和Json,在输入中输入数据后更新div。不行

PHP和Json,在输入中输入数据后更新div。不行,php,jquery,json,Php,Jquery,Json,用户在输入中输入数据后,我需要更新div,但我不能。div出现,但随后消失 我的代码: function.js window.onload = function(){ var mydata = document.getElementsByTagName("input"); if(mydata != null){ for(var i=0; i< mydata.length; i++) { if(mydata[i].type == "text"){

用户在输入中输入数据后,我需要更新div,但我不能。div出现,但随后消失

我的代码:

function.js

window.onload = function(){
var mydata = document.getElementsByTagName("input");
if(mydata != null){
    for(var i=0; i< mydata.length; i++) {
        if(mydata[i].type == "text"){
            if(mydata[i].id == "qtd") {
                mydata[i].onchange = function () {
                    send(this);
                };
            }
        }
    }
}   
}

function send(box){
if (box == null) { return; }
var nameBox = box.name;
var url="process.php?nameBox="+nameBox+"&value="+encodeURIComponent(box.value);
requisicaoHTTP("GET",url,true);
}

function dataNew(){
var info = ajax.responseText;
if(info != null){
    var out= document.getElementById("myDIV");
    out.innerHTML = info;
}
}
window.onload=function(){
var mydata=document.getElementsByTagName(“输入”);
if(mydata!=null){
对于(var i=0;i
HTML:



我希望在输入中输入数据后显示并更新div。

您可以使用jQuery中的.html函数

(请记住将jQuery添加到代码中)


更新

因此,代码的完整函数应该如下所示:

HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
   <script type="text/javascript" src="functions.js"></script>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
  <form id="txt_form" action="">
    <input type="text" name="test" id="test" value="">
    <input type="submit" value="Submit" id="txt_send">
  </form>
  <div id="myDIV"></div>
 </body>
 </html>

我注释掉了Ajax,因为在fiddle上当然没有php。

你在哪里调用你的dataNew函数?在Ajax.js函数response(){if(Ajax.readyState==4){if(Ajax.status==200){dataNew();}else{alert(“not work XMLHttpRequest.”;}}所以我更新了我的示例,它应该适合你。。。希望能有帮助
$('#txt_box').keyup(function(event){
        // Get the typed text
  var txt = $(this).val();

  // Insert the text to the div
  $("#myDIV").html(txt);
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
   <script type="text/javascript" src="functions.js"></script>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
  <form id="txt_form" action="">
    <input type="text" name="test" id="test" value="">
    <input type="submit" value="Submit" id="txt_send">
  </form>
  <div id="myDIV"></div>
 </body>
 </html>
$("#txt_send").click(function(event){
    event.preventDefault();
    var field_name = $("#test").attr("name");
    var field_val = $("#test").val();

    // POST to update the text.
      $.ajax({
        type: "POST",
        url: "process.php?nameBox="+field_name+"&value="+encodeURIComponent(field_val),
        dataType: "html"
      }).done(function(data) {
        // Display the text on the div.
        $("#myDIV").html(field_val);
       }
      });
    });
});