0

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

这是阿贾克斯:

    $(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-fieldandeditor-label <div>元素附加到items.

4

1 回答 1

1

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

$("#items").append($(html).find(".label-field, .editor-label"));
于 2013-05-29T22:12:39.407 回答