只在Ajax成功函数中附加某些类的div

时间:2013-05-29 22:09:48

标签: ajax asp.net-mvc-3 jquery asp.net-ajax

我有一个局部视图,用于将一堆html返回到我的主页面。在我的Ajax调用中,我需要获取该局部视图,但我只想追加某些<div>元素。这可能吗?

这是Ajax:

    $(document).ready(function () {
        $("#addItem").click(function () {
            $.ajax({
                url: '@Url.Action("BlankDropDownItem", "DropDownValues")',
                data: { field: $('#Field').val(), displayPage: $('#DisplayPage').val() },
                dataType: 'html',
                cache: false,
                success: function (html) {
                    $("#items").append(html);
                }
            });                
            return false;
        });
    });

这将调用ASP MVC控制器方法,该方法返回此部分视图。

    <div id="LeftDiv" style="width:250px;float:left;">
        <fieldset>
            <legend>Category Information</legend>
            <div class="M-editor-label">
                @Html.LabelFor(model => model.Field)
            </div>
            <div class="M-editor-field">
                @Html.EditorFor(model => model.Field)
                @Html.ValidationMessageFor(model => model.Field)
            </div>
            <div class="M-editor-label">
                @Html.LabelFor(model => model.DisplayPage)
            </div>
            <div class="M-editor-field">
                @Html.EditorFor(model => model.DisplayPage)
                @Html.ValidationMessageFor(model => model.DisplayPage)
            </div>
        </fieldset>
    </div>
    <div id="RightDiv" style="width:300px;float:left; padding-left:50px;">
        <fieldset id="items">
            <legend>Drop Down Items</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.AllowedValue)
            </div>
            <div class="label-field">
                @Html.EditorFor(model => model.AllowedValue)
                @Html.ValidationMessageFor(model => model.AllowedValue)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.DisplayValue)
            </div>
            <div class="label-field">
                @Html.EditorFor(model => model.DisplayValue)
                @Html.ValidationMessageFor(model => model.DisplayValue)
            </div>
        </fieldset>
     </div> 

但是,我只想将label-fieldeditor-label <div>元素附加到items

1 个答案:

答案 0 :(得分:1)

您也可以使用jQuery搜索返回的html。效率不高,但可能适合您的目的。

$("#items").append($(html).find(".label-field, .editor-label"));