Javascript 如何在输入更改时发布到php,然后在contentdiv中获得结果

Javascript 如何在输入更改时发布到php,然后在contentdiv中获得结果,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我想按照标题所说的做,当数据输入到文本字段时,我想将其发送到posttothis.php,然后将结果打印在内容分区中。我似乎无法使其工作 testscript.html <html> <head> <script type="text/javascript"> document.write("\<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'&

我想按照标题所说的做,当数据输入到文本字段时,我想将其发送到posttothis.php,然后将结果打印在内容分区中。我似乎无法使其工作

testscript.html

<html>
<head>
<script type="text/javascript">
document.write("\<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'>\<\/script>");
</script>
</head>
<body>
<input type="text" name="text" id="text">
<div id="content" name="content">
<script>
$('#text').keyup(function(){
$.ajax({
    type: "POST",
    url: "posttothis.php",
    data: {text: $(this).val()},
    success: function(data)
            {
                $("#content").html(data);
            }
    });
});
</script>
</body>
</html>

文件。写“\\”;
$('#text').keyup(函数(){
$.ajax({
类型:“POST”,
url:“posttothis.php”,
数据:{text:$(this).val()},
成功:功能(数据)
{
$(“#content”).html(数据);
}
});
});
posttothis.php

<?php
$testvar=$_POST['text'];
echo $testvar;
?>

谢谢

编辑:用建议的修改更新了我的脚本,但仍然无法让它显示我在content div的文本框中键入的内容。要回答以下问题:我不想提交表单,我只想在每次更改时从中获取值。控制台显示没有错误

EDIT2:我更新了工作代码,也许它将来会帮助其他人。

使用:

$('#text').keyup(function(){
而不是

$('#text').change(function(){
此外,数据属性后缺少逗号(,)

data: {text: $('#text').val()},
使用:

而不是

$('#text').change(function(){
此外,数据属性后缺少逗号(,)

data: {text: $('#text').val()},
为什么不试试呢

范例

   <input type="text" name="text" id="text">
    <div id="content" name="content">
    <script>
    $('#text').keyup(function(){
        $.post('posttothis.php', {text:$('#text').val()}, function(data) {
            $("#content").html(data);
        });
    });
</script>

$('#text').keyup(函数(){
$.post('posttothis.php',{text:$('#text').val()},函数(数据){
$(“#content”).html(数据);
});
});
为什么不试试

范例

   <input type="text" name="text" id="text">
    <div id="content" name="content">
    <script>
    $('#text').keyup(function(){
        $.post('posttothis.php', {text:$('#text').val()}, function(data) {
            $("#content").html(data);
        });
    });
</script>

$('#text').keyup(函数(){
$.post('posttothis.php',{text:$('#text').val()},函数(数据){
$(“#content”).html(数据);
});
});
,“放在数据之后:{text:$('#text').val()}


$('#text')。更改(函数(){
$.ajax({
类型:“POST”,
url:“posttothis.php”,
数据:{text:$('#text').val()},
成功:功能(数据)
{
$(“#content”).html(数据);
}
});
});
,“放在数据之后:{text:$('#text').val()}


$('#text')。更改(函数(){
$.ajax({
类型:“POST”,
url:“posttothis.php”,
数据:{text:$('#text').val()},
成功:功能(数据)
{
$(“#content”).html(数据);
}
});
});
您可以改用该功能

<input type="text" name="text" id="text">
<div id="content" name="content">
<script>
$('#text').keyup(function(){
$.ajax({
    type: "POST",
    url: "posttothis.php",
    data: {text: $(this).val()},
    success: function(data)
            {
                $("#content").html(data);
            }
    });
});
</script>

$('#text').keyup(函数(){
$.ajax({
类型:“POST”,
url:“posttothis.php”,
数据:{text:$(this).val()},
成功:功能(数据)
{
$(“#content”).html(数据);
}
});
});
您可以改用该功能

<input type="text" name="text" id="text">
<div id="content" name="content">
<script>
$('#text').keyup(function(){
$.ajax({
    type: "POST",
    url: "posttothis.php",
    data: {text: $(this).val()},
    success: function(data)
            {
                $("#content").html(data);
            }
    });
});
</script>

$('#text').keyup(函数(){
$.ajax({
类型:“POST”,
url:“posttothis.php”,
数据:{text:$(this).val()},
成功:功能(数据)
{
$(“#content”).html(数据);
}
});
});

success:function和see-data是如何从没有“form-tag”的“testscript.html”中发布值的?Javascript控制台(开发工具、Firebug等)告诉您什么?在没有“form-tag”的情况下,数据属性try
alert(data)
success:function和see-data是如何从“testscript.html”中发布值的?您的Javascript控制台(开发工具、Firebug等)告诉您什么?您在success:function中的数据属性try
alert(data)
后缺少一个逗号,请查看数据是否为fetch?您的数据参数不足。。。它应该像
{“text”:$('#text').val()}
或只是
“text=“+$('#text').val()
您的数据参数不足。。。它应该像
{“text”:$('#text').val()}
或只是
“text=“+$('#text').val()