Javascript 如何使用 jquery/ajax 刷新 div 中的表格内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30254977/
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 04:52:04  来源:igfitidea点击:

How to refresh table contents in div using jquery/ajax

javascriptjqueryajaxxhtml

提问by 99maas

I need your help in order to refresh a div id="mytable"in my html once the function is called from a method. Currently, I am loading the full page once it is called using the below lines.

id="mytable"一旦从方法调用该函数,我需要您的帮助才能刷新我的 html 中的 div 。目前,一旦使用以下几行调用它,我就会加载整个页面。

In my java method, I am using the below line to call a javascript method:

在我的 java 方法中,我使用以下行来调用 javascript 方法:

RequestContext.getCurrentInstance().execute("autoRefresh()"); 

The html code :

html代码:

<script type="text/javascript">
    function autoRefresh() {
        window.location.reload();
    }
</script>

<div id='mytable'>
    <h1 id='My Table'>
        <table></table>
    </h1>
</div>

回答by Ary Wibowo

You can load HTML page partial, in your case is everything inside div#mytable.

您可以部分加载 HTML 页面,在您的情况下是 div#mytable 中的所有内容。

setTimeout(function(){
   $( "#mytable" ).load( "your-current-page.html #mytable" );
}, 2000); //refresh every 2 seconds

more information read this http://api.jquery.com/load/

更多信息阅读这个http://api.jquery.com/load/

Update Code (if you don't want it auto-refresh)

更新代码(如果您不希望它自动刷新)

<button id="refresh-btn">Refresh Table</button>

<script>
$(document).ready(function() {

   function RefreshTable() {
       $( "#mytable" ).load( "your-current-page.html #mytable" );
   }

   $("#refresh-btn").on("click", RefreshTable);

   // OR CAN THIS WAY
   //
   // $("#refresh-btn").on("click", function() {
   //    $( "#mytable" ).load( "your-current-page.html #mytable" );
   // });


});
</script>