Clear html content with jquery not working

Asked

Viewed 144 times

0

I have this jquery

<script type="text/javascript">
    $(document).ready(function () {
        $("#GrupoDescontos").change(function () {
            $.ajax
            ({
                url: '' + $(this).val(),
                type: 'GET',
                success: function (dados) {
                    $("#GridPartial").html("");
                    $("#GridPartial").html(dados);
                    var resultado = dados;
                },
                error: function (erro) {

                }
            });
        });
    });

When I first enter the screen, the Detail screen is loaded. after selecting the Dropdown the grid is reloaded, but the problem is the screen that enters for the first time, without the change of the Dropdown, is not cleared by the command of jquery $("#GridPartial").html("");, getting two grids chained and is loaded duplicate information from the first screen that does not exist in the partial view, as title, for example. How I clean it up?

EDIT1

See how the screen is all scrambled with two grids inserir a descrição da imagem aqui

EDIT2

View

@model Site.Areas.API.Models.AzureDiscountGroups.AzureDiscountGroupModel
@using SubscriptionCenter.API.Models.Resellers


<style>
    .Makewide {
        width: 200px;
    }
</style>

<div>
    <h4></h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.Id)

    <div class="form-group">
        @Html.Label("Grupo de Desconto", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("GrupoDescontos", new SelectList(ViewBag.Desconto, "Id", "Descricao"), new { @id = "GrupoDescontos", @class = "Makewide" })
            @*@Html.ValidationMessageFor(model => model.HomeTemplateId, "", new { @class = "text-danger" })*@
        </div>
    </div>

</div>

<div id="GridPartial"></div>

@Html.Partial("DetailsPartial")

<p>
    @Html.ActionLink("Back to List", "Index")
</p>

<script type="text/javascript">
    $(document).ready(function () {
        $("#GrupoDescontos").change(function () {
            var $div = $('#GridPartial');
            $.ajax
            ({
                url: '' + $(this).val(),
                type: 'GET',
                    success: function (dados) {
                        $div.html('');
                        $div.html(dados);
                },
                    error: function (erro) {
                }
            });
        });
    });

    $(document).ready(function () {
        $("#GrupoDescontos").val("@ViewBag.Indice");
    });
</script>

and the Partialview

<table class="table">
    <tr>
        <th>
            Nome
        </th>
        <th>
            Alias
        </th>
        <th>
            WhiteLabel?
        </th>
        <th>
            MPN Id
        </th>
        <th>
            Criada em
        </th>
    </tr>

    @foreach (var item in ViewBag.DetailReseller as List<ResellerListModel>)
    {
        <tr align="center">
            <td>
                @(item.Name)
            </td>
            <td>
                @(item.Alias)
            </td>
            <td>
                @(item.WhiteLabel ? "Sim" : "Não")
            </td>
            <td>
                @(item.MpnId)
            </td>
            <td>
                @(item.CreatedOn)
            </td>
        </tr>
    }
</table>
  • has tried to give a console.log ("test") there to know if the code is coming to it?

2 answers

3


The problem is that your div is empty, the content you want to replace is out of it, so the content is duplicated when it comes from partialView. For the content of partial to be replaced do as an example below, placing all content of partial into the div updated on ajax:

<div id="GridPartial">
    @Html.Partial("DetailsPartial")
</div>

In the ajax, gets like this:

$(document).ready(function () {
    $('#GrupoDescontos').change(function () {
        var $div = $('#GridPartial');
        $.ajax
        ({
            url: '' + $(this).val(),
            type: 'GET',
            success: function (dados) {
                $div.html(dados);
            },
            error: function (erro) {
                alert('Ops...');
            }
        });
    });
});
  • George, I couldn’t clean up. As I said the first screen does not come from jquery, but from Index but populated with Partial View, I do not know if this has anything to do, but it is as an observation

  • @pnet, post the cshtml of your view and partial... is the same as your other question (https://answall.com/q/373083/35358)?

  • @pnet, I know, I’ve noticed the problem, it’s in html... updated the answer hehe

  • Dude, I got to put a grid only, show. The header(Dropdown) and the Footer are repeating, you know what can be?

  • @pnet That’s why the html that comes in your ajax comes with the header tbm... since the full page is coming you should put all the content inside your div

  • You’re right, it worked. Marking and closing the post

  • Strange, last night I had scheduled George’s answer and today when I opened Sopt, the answer was not marked.

  • Strange really, @pnet didn’t notice anything here, for me it is marked since yesterday

Show 3 more comments

1

Hello, good night!

The command to clear is:

$("#GridPartial").empty();
$("#GridPartial").html("seu conteúdo");
  • But I’ve done it and nothing happened

  • I did it and didn’t clean it: $("#GridPartial").empty(dados);

  • Take a look at this example: [link] "https://www.w3schools.com/jquery/html_empty.asp" Voce

  • But that’s what I did and nothing. What happens is this: The first load comes from index, I select a group in index and click on Details then open the Detail screen with the Reseller of that selected group. Inside of Detail I can select another Group and show in the grid the new resellers of that group. This second time is done by jquery(ajax), the problem that the first content continues on the screen and jquery adds another one on top without clearing the previous.

  • Please check if you have two identical id’s... if you have, change the id and see if it will work...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.