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

标签 php jquery json

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

我的代码:

函数.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;
}
}

HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
   <script type="text/javascript" src="Ajax.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 action="javascript:void%200">
    <input type="text" name="test" id="test" value="">
  </form>
  <div id="myDIV"></div>
 </body>
 </html>

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

最佳答案

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

http://api.jquery.com/html/

$('#txt_box').keyup(function(event){
        // Get the typed text
  var txt = $(this).val();

  // Insert the text to the div
  $("#myDIV").html(txt);
});

Fiddle Example

(记住将 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>

函数.js

$("#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);
       }
      });
    });
});

Full Code Fiddle Example 我注释掉了 Ajax,因为 fiddle 上当然没有 php。

关于PHP和Json,在input中输入数据后更新div。不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37060297/

相关文章:

php - PHP 中的回发

PHP 扩展 zip,ZipArchive 不适用于命名空间

javascript - 如何防止滚动事件多次触发?

javascript - 生成的按钮失去功能

JavaScript 无法在我的服务器上运行

php - 比较 mysql 表中的最新和最早日期并仅显示更改

php - 导航菜单的 Magento SSL 问题

javascript - 根据日期和时间显示打开或关闭

javascript - 如何获取分组 Kendo 网格中选定行的索引和数据

javascript - 搜索最大的 id 并在 JSON 文件中添加新的最大 id ( NodeJS )