Javascript 是否可以在不刷新整个页面的情况下加载页面内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37866519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:49:28  来源:igfitidea点击:

Is it possible to load content of page without Refreshing the whole page

javascriptphpjqueryhtmlajax

提问by Dhruv Tyagi

Actually i want to refresh my content of a page without Refreshing the whole page through JavaScript or j Query ....... and i did my whole project into ( Php or javaScript) so i face such type of problem

实际上我想刷新我的页面内容而不通过 JavaScript 或 j Query 刷新整个页面......我把我的整个项目都变成了(Php 或 javaScript)所以我面临这样类型的问题

Note :i want to refresh my page content when user do some action here is the screenshot of the page i want to load these dynamic boxes without loading the whole page

注意:我想在用户执行某些操作时刷新我的页面内容 这是我想加载这些动态框而不加载整个页面的页面的屏幕截图

Here is my Code:

这是我的代码:

//On Button click, the below will be execute:

//在Button点击时,将执行以下内容:

  $('body').on('click', '#click', loadDoc); 

and the LoadDoc functio:

和 LoadDoc 函数:

   function loadDoc() { 

                                //alert('heruybvifr');
                var _this = $(this); 
                var order_id= $(this).parents('.modal').find('.order-id').text(); 

                $.get('myPHP.php',{order_id: order_id},function(){ 
             _this.hide();

                }) 
                }

Now myPHP.php :

现在 myPHP.php :

  <?php
 include("connection.php");
 $limit = intval($_GET['order_id']);

echo $valuek;
    $query="UPDATE orders
SET status ='cooking'
 WHERE id = $limit";
if (mysqli_query($connection,$query)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($connection);
 }


  ?> 

回答by Buddy

Yes you can use the jQuery.ajax()call. Like this: Change the text of a element using an AJAX request:

是的,您可以使用jQuery.ajax()电话。像这样:使用 AJAX 请求更改元素的文本:

$("button").click(function(){
    $.ajax({url: "demo_test.txt", success: function(result){
        $("#div1").html(result);
    }});
});

See this tutorial for more information: http://www.w3schools.com/jquery/ajax_ajax.asp

有关更多信息,请参阅本教程:http: //www.w3schools.com/jquery/ajax_ajax.asp

回答by Asmita

You can use JQuery Ajax functions to accomplish your requirement. all there functions given below will work for loading the content without refreshing the page.

您可以使用 JQuery Ajax 函数来完成您的要求。下面给出的所有功能都可以在不刷新页面的情况下加载内容。

    $.post("/controller/function", params, function(data) {
        // set received data to html
    });

$.ajax("/controller/function", params, function(data) {
        // set received data to html
    });

$.get("/controller/function", params, function(data) {
        // set received data to html
    });

回答by Kld

You can load the data from the server and and place the returned HTML into the matched element.

您可以从服务器加载数据并将返回的 HTML 放入匹配的元素中。

<div id="content"></div>

$("#content").load( "ajax/test.html" );