繁体   English   中英

如何使用ajax刷新刷新div?

[英]How to refresh div using ajax refresh?

我正在开发spring mvc controller项目,我将数据从控制器传递给jsp。 我的jsp页面有水平标签设计,点击每个标签后,我正在显示表格。

目前我的设计中只有两个标签。 单击TEST1选项卡后,它会显示Table1但如果单击TEST2选项卡,则会显示另一个具有不同列和数据的表,即Table2 ,它可以正常工作。

这是我的jsfiddle

这是我的test4.jsp设计 -

<div id='headerDivDash'>
    <h1 id='topHeaderDash'>
        some_image
    </h1>
</div>

<ul id="tabs">
    <li><a href="#" name="tab1">TEST1</a></li>
    <li><a href="#" name="tab2">TEST2</a></li>
</ul>

<div id="content"> 
    <div id="tab1">
        <h2>Test 1</h2>
        <div class="container">
            <c:forEach var="e" items="${testing.machines}">
                <div class="component">
                    <h3>
                        For
                        <c:out value="${e.key}" />
                    </h3>
                    <table class="overflow-y">
                        <thead>
                            <tr>
                                <th>First Name</th>
                                <th>Last Name</th>
                                <th>Address</th>
                                <th>Email</th>
                            </tr>
                        </thead>
                        <tbody>
                            <c:forEach var="m" items="${e.value}">
                                <tr>
                                    <th>${m.fName}</th>
                                    <td class="color-changer">${m.lName}</td>
                                    <td>${m.address}</td>
                                    <td>${m.email}</td>
                                </tr>
                            </c:forEach>
                        </tbody>
                    </table>
                </div>
            </c:forEach>
        </div>

    </div>
    <div id="tab2">
            <h2>Test 2</h2>

        <div class="container">
            <c:forEach var="e" items="${testing.machines}">
                <div class="component">
                    <h3>
                        For
                        <c:out value="${e.key}" />
                    </h3>
                    <table class="overflow-y">
                        <thead>
                            <tr>
                                <th>First Name</th>
                                <th>Last Name</th>
                                <th>Address</th>
                                <th>Email</th>
                            </tr>
                        </thead>
                        <tbody>
                            <c:forEach var="m" items="${e.value}">
                                <tr>
                                    <th>${m.fName}</th>
                                    <td class="color-changer">${m.lName}</td>
                                    <td>${m.address}</td>
                                    <td>${m.email}</td>
                                </tr>
                            </c:forEach>
                        </tbody>
                    </table>
                </div>
            </c:forEach>
        </div> 
    </div>
</div>
<div class="footer">&copy Some value</div>

我将数据从控制器传递到jsp页面。 现在我想做的是 -

  • 我想使用AJAX刷新每30秒刷新一次tab1 divtab2 div
  • 此外,如果我在TEST1选项卡上,然后刷新正在进行,那么它应该在TEST1选项卡上,但假设我在TEST2选项卡上,然后刷新正在发生,那么它应该在TEST2选项卡上。

下面是我提出的jquery只是为了tab1刷新不知道我如何将它用于tab2 ,但不知何故刷新之后,我仍然看到旧数据并且它在刷新后没有显示最新数据。

$(document).ready(function(){
    // Create overlay and append to body:
    $('<div id="overlay"/>').css({
        position: 'fixed',
        top: 0,
        left: 0,
        width: '100%',
        height: $(window).height() + 'px',
        opacity:0.4, 
        background: 'lightgray url(http://bradsknutson.com/wp-content/uploads/2013/04/page-loader.gif) no-repeat center'
    }).hide().appendTo('body');

    // Execute refresh with interval:
    setInterval(refresh, 30 * 1000);
});

//Create a refresh function:
function refresh(){
    // SHOW overlay
    $('#overlay').show();
    // Retrieve data:
    $.ajax({
        url: 'test4.jsp',
        type: 'GET',
        success: function(data){
            var content =  $($.parseHTML(data)).filter("#tab1");
            //Replace content inside the div
            $('#tab1').replaceWith(content); # this is not updating my tab div with latest data
            // HIDE the overlay:
            $('#overlay').hide();
        }
    });
}

我无法看到我的tab1 div正在刷新。 我可以看到刷新图像显示,但不知何故tab1始终保持相同的旧数据,我看到第一次击中网址..我的jquery有什么问题吗?

另外我注意到一件事很奇怪,如果我删除content div and tab1 div并且只刷新container div然后我能够在刷新后看到我的更新数据并且它工作正常但是如果我添加content and tab1 div并尝试刷新container div ,然后它根本不起作用。 所以有些事情是肯定的。

我怀疑这种情况正在发生,因为我的CSS问题,我想这是在jsfiddle中显示的。

任何人都可以帮我吗?

首先,当你试图获取test4.html时,它在ajax上给出404错误,所以我使用test(jsfiddle默认的ajax模型)。 我可以在tab1上看到更新,关于tab2,如果你在ajax成功中替换“#tab1”中的内容,它将不会更新tab2内容。

第二个donot使用replaceWith()这将替换完整元素所以下次没有div元素id =“tab1”..而是使用这个..

        if( $("#tabs li:first").attr("id") ==="current"){
        $('#tab1').html("dummy content 1");
        }
        else { $('#tab2').html("dummy content 2"); } 

小提琴演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM