Javascript 如何在提交时加载div而不使用ajax刷新页面?

Javascript 如何在提交时加载div而不使用ajax刷新页面?,javascript,php,ajax,Javascript,Php,Ajax,这是我的代码,工作正常,但每次提交时都必须重新加载。我想在每次单击提交按钮时重新加载comments.php 我应该用JavaScript写什么 Index.php: 提交 //我想在每次提交时重新加载 函数myfunc(){ $.ajax({ url:“comments.php”, 数据:$(“输入”).val(), 方法:'post', 成功:功能(响应){ }, 错误:函数(错误、消息){ } }); } 这是使用jquery函数myfunc()实现的{ $.ajax({ url:

这是我的代码,工作正常,但每次提交时都必须重新加载。我想在每次单击提交按钮时重新加载
comments.php

我应该用JavaScript写什么

Index.php:



提交 //我想在每次提交时重新加载
函数myfunc(){
$.ajax({
url:“comments.php”,
数据:$(“输入”).val(),
方法:'post',
成功:功能(响应){
},
错误:函数(错误、消息){
}
});
}
这是使用jquery

函数myfunc()实现的{
$.ajax({
url:“comments.php”,
数据:$(“输入”).val(),
方法:'post',
成功:功能(响应){
},
错误:函数(错误、消息){
}
});
}

这是关于jquery的

这个问题使用jquery。如果您不知道,请访问以了解更多信息:

在comments.php周围添加一个div:

<div id="comments">
  <?php include("comments.php") ; ?> 
</div>

现在,您可以捕获submit并用ajax调用替换它,然后重新加载注释:

<script>
 $(document).ready(function(){
  $("#reply").on("submit",function(e){
    e.preventDefault();
    $.ajax();//todo for you. I cannot write this with this rare information
    $("#comments").load("comments.php");//the magic part
    return false;//prevent the real submit
  }):
});
</script>

$(文档).ready(函数(){
$(“#答复”)。关于(“提交”,职能(e){
e、 预防默认值();
$.ajax();//你要做的事。我不能用这种罕见的信息来写这篇文章
$(“#comments”).load(“comments.php”);//神奇的部分
return false;//防止实际提交
}):
});

此问题使用jquery。如果您不知道,请访问以了解更多信息:

在comments.php周围添加一个div:

<div id="comments">
  <?php include("comments.php") ; ?> 
</div>

现在,您可以捕获submit并用ajax调用替换它,然后重新加载注释:

<script>
 $(document).ready(function(){
  $("#reply").on("submit",function(e){
    e.preventDefault();
    $.ajax();//todo for you. I cannot write this with this rare information
    $("#comments").load("comments.php");//the magic part
    return false;//prevent the real submit
  }):
});
</script>

$(文档).ready(函数(){
$(“#答复”)。关于(“提交”,职能(e){
e、 预防默认值();
$.ajax();//你要做的事。我不能用这种罕见的信息来写这篇文章
$(“#comments”).load(“comments.php”);//神奇的部分
return false;//防止实际提交
}):
});
您可以使用jQuery load()方法

jQuery load()方法从服务器加载数据,并将返回的HTML放入所选元素中。此方法提供了一种从web服务器异步加载数据的简单方法

load()方法的参数具有以下含义:

  • 必需的URL参数指定要删除的文件的URL 装载
  • 可选数据参数指定一组查询字符串 (即键/值对)与 请求
  • 可选的complete参数基本上是一个回调函数 在请求完成时执行。回调被触发 每个选定元素一次
  • 您的html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){
            $("#box").load("comments.php");
        });
    });
    </script>
    </head>
    <body>
        <div id="comments">
    
        </div>
        <button type="button">Load Comments</button>
    </body>
    </html>
    
    
    $(文档).ready(函数(){
    $(“按钮”)。单击(函数(){
    $(“#box”).load(“comments.php”);
    });
    });
    加载注释
    
    Comments.php

    <?php
    
    
        //content here
    
        echo "I'm loaded";
    
        ?>
    
    您可以使用jQuery load()方法

    jQuery load()方法从服务器加载数据,并将返回的HTML放入所选元素中。此方法提供了一种从web服务器异步加载数据的简单方法

    load()方法的参数具有以下含义:

  • 必需的URL参数指定要删除的文件的URL 装载
  • 可选数据参数指定一组查询字符串 (即键/值对)与 请求
  • 可选的complete参数基本上是一个回调函数 在请求完成时执行。回调被触发 每个选定元素一次
  • 您的html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){
            $("#box").load("comments.php");
        });
    });
    </script>
    </head>
    <body>
        <div id="comments">
    
        </div>
        <button type="button">Load Comments</button>
    </body>
    </html>
    
    
    $(文档).ready(函数(){
    $(“按钮”)。单击(函数(){
    $(“#box”).load(“comments.php”);
    });
    });
    加载注释
    
    Comments.php

    <?php
    
    
        //content here
    
        echo "I'm loaded";
    
        ?>
    

    你用谷歌搜索过吗?如果你不想刷新页面,首先更改type=“button”而不是“submit”可能重复的@ppasler我问如何在submit上加载特定的div。。。。。。我并没有问过如何在不刷新整个表单的情况下提交表单page@truespeaker为什么你没有回复答案,而是回复评论?你用谷歌搜索过吗?如果你不想刷新页面,首先更改type=“button”而不是“submit”可能重复的@ppasler我问如何在submit上加载特定的div。。。。。。我并没有问过如何在不刷新整个表单的情况下提交表单page@truespeaker为什么你不回应答案,而是回应评论?