如何用Ajax响应中的javascript替换内容

时间:2016-04-13 06:57:00

标签: javascript ajax asp.net-mvc

我不想用索引视图中的div替换部分View中的新数据。我得到了正确的答案,我在萤火虫中看到了。现在我只在成功时显示一个警告窗口,表示它正在工作。 Bellow是我的Ajax代码,也是我得到的响应的一部分(ul中有20个list-group项)。那么我是否必须为每个div javascript代码编写,就像在我的视图中那样,还是有其他选择?可能还有一些例子吗?

问候!

<script>
        function testAjax() {
            debugger;
            $.ajax({
                url: "@Url.Content("~/Controller/MyAction")",
                data: "searchString=search",
                type: "Post",
                beforeSend: function () {
                    //Code for before send whatever you have to do.
                },
                success: function (data) {
                    alert("It's Working!");
                },
                error: function (response) {
                    //Your code when error ocure
                    alert(response.responseText);
                },
                failure: function (response) {
                    //your code for failure
                    alert(response.responseText);
                }
            })
        }
    </script>

我的回复:

<ul class="list-group infinite-scroll">                       
        <li class="list-group-item col-md-6">
            <div class="noo-event-featured-item grow">
                <div class="sc-event-content">
                    <h3><a href="/Controller/Podrobnosti/73" onclick="checkCookie();">text</a></h3>
                    <div class="bottom">
                        <div class="create-date">
                            <span>
                                date
                            </span>
                        </div>
                    </div>
                </div>
                <div class="sc-event-item">
                    <div class="event-thumbnail">
                        <a href="link" >
                            <img src="image link">
                        </a>
                        <div class="sc-meta">
                            <span class="sc-date"><i class="fa fa-clock-o"></i><span> text</span></span>
                            <span class="sc-address">
                                <i class="fa  fa-map-marker" style="padding-left: 3px;"></i>Place<span class="tribe-address">
                                    <span class="tribe-street-address">Street</span><span class="tribe-delimiter"
>, </span>
                                    <span class="tribe-locality">Country</span>
                                </span>
                            </span>                         
                        </div>
                    </div>
                </div>               
            </div>
        </li>
</ul>

2 个答案:

答案 0 :(得分:0)

如果你的回复是一个HTML代码(纯文本),你可以尝试在你的成功函数中使用这样的东西:

$('yourDiv').html(data);//replace child nodes

答案 1 :(得分:0)

谢谢大家,我已经弄明白了。你必须在divid $(".divid").replaceWith(data);

之前使用一个点