【问题标题】:refresh only one div只刷新一个div
【发布时间】:2015-03-04 22:51:49
【问题描述】:

我有一个使用 hibernate to mysql 的 webapp。 webapp 工作正常,如果我的数据库获得新值,它们会立即出现在页面的 刷新后 的 webapp 上。我想摆脱我的“每五秒刷新一次整个页面”构建,并有一个复选框,当激活 tbody 中的列表时,它将单独刷新。我也在 div 上尝试过自动刷新,但是,是的,如果你们中的任何人知道这一点,请帮忙!谢谢!

代码块 1(头部):

<script type="text/javascript">
function auto(){setInterval(
function () { $('#doRefresh'). ????

;}, 2000);  } </script>

代码块 2(div 1):

<input type='button' value='UPDATE' class="btn" onclick="auto()"/>

代码块 3(div 2):

<tbody id="doRefresh">
        <c:forEach var="sensor" items="${listSensors}" varStatus="status">
            <tr class="listData">
                <td>${sensor.fileName}</td>
                <td>${sensor.date}</td>
                <td>${sensor.time}</td>
                <td>${sensor.channel1}</td>
                <td>${sensor.channel2}</td>
                <td>${sensor.channel3}</td>
                <td>${sensor.channel4}</td>
                <td>${sensor.channel5}</td>
                <td>${sensor.channel6}</td>
                <td>${sensor.channel7}</td>
                <td>${sensor.channel8}</td>               
            </tr>
        </c:forEach>    
    </tbody>

为了获得灵感(以及更多),我访问了以下页面:

1. https://wowjava.wordpress.com/2010/02/06/auto-refreshing-the-content-
without-reloading-page-using-jquery/
2. http://www.sitepoint.com/auto-refresh-div-content-jquery-ajax/
3. http://crunchify.com/how-to-refresh-div-content-without-reloading-page-
using-jquery-and-ajax/
4. http://stackoverflow.com/questions/5987802/refreshing-only-one-div-in-
page-with-just-regular-javascript

谢谢

【问题讨论】:

    标签: javascript jquery ajax jsp


    【解决方案1】:

    由于您已经拥有可用于生成 html 的服务器代码,您可以使用最简单的 jQuery ajax 方法,即load()

    方法通过简单地传入一个 url 将选择器中的所有 html 替换为来自服务器的新 html。

    /* within interval function */
     $('#doRefresh').load('path/to/server/script', function(){
         /* new html has been inserted, can run any html related code here*/
     });
    

    由于&lt;tbody&gt; 没有被替换,只有行,只是发回行。否则使用$('#doRefresh').parent().load(... 保留&lt;tbody&gt;

    参考:load() Docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      相关资源
      最近更新 更多