javascript - 删除客户端的最后一个查询

标签 javascript jquery

我有一个下拉列表,根据所选的单选按钮填充:

JS

 $(function getJsonProveedores() {
        var items = "";
        $('input[type=radio][name=editList]').change(function () {
            if (this.value == 'Proveedor') {
                $.getJSON("@Url.Action("GetProveedores", "Agenda")", function (data) {

                    $.each(data, function (index, item) {
                        items += "<option value='" + item.ID + "'>" + item.NombreComercial + "</option>";
                    });
                    $("#lstProveedor").html(items);
                });
            }
            else if (this.value == 'Sucursal') {
                $.getJSON("@Url.Action("GetUnidades", "Agenda")", function (data) {

                    $.each(data, function (index, item) {
                        items += "<option value='" + item.ID + "'>" + item.Nombre + "</option>";
                    });
                    $("#lstProveedor").html(items);
                });

            } else if (this.value == 'Usuario') {
                $.getJSON("@Url.Action("GetUsuario", "Agenda")", function (data) {

                    $.each(data, function (index, item) {
                        items += "<option value='" + item.ID + "'>" + item.Nombre + "</option>";
                    });
                    $("#lstProveedor").html(items);
                });
            }
        });

    });

它正确加载所有 JSON,但我在删除最后一个查询时遇到问题,例如,如果我单击 Proveedor radio,它会加载:

$.getJSON("@Url.Action("GetProveedores", "Agenda")"

接下来,如果我单击 Sucursal radio,它就会加载

$.getJSON("@Url.Action("GetUnidades", "Agenda")"

问题是我一直看到

的第一个查询结果
$.getJSON("@Url.Action("GetProveedores", "Agenda")"

而不是只得到第二个:

$.getJSON("@Url.Action("GetUnidades", "Agenda")"

Controller :

public ActionResult GetProveedores()
        {
            var listaProveedores = db.Proveedores.ToList();
            return Json(listaProveedores, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetUnidades()
        {
            var listaUnidades = db.Unidades.ToList();
            return Json(listaUnidades, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetUsuario()
        {
            var listaUsuarios = db.Usuarios.ToList();
            return Json(listaUsuarios, JsonRequestBehavior.AllowGet);
        }

那里可能有什么问题?问候

最佳答案

按照函数/变量的结构方式,items 永远不会超出范围。所以它永远不会被重置。每个后续 AJAX 调用都只是附加到它上面。

相反,从当前作用域中删除 items 变量,并在需要的每个作用域中使用一个本地变量:

$(function getJsonProveedores() {
    $('input[type=radio][name=editList]').change(function () {
        if (this.value == 'Proveedor') {
            $.getJSON("@Url.Action("GetProveedores", "Agenda")", function (data) {
                var items = "";  // <--- declare it here
                $.each(data, function (index, item) {
                    items += "<option value='" + item.ID + "'>" + item.NombreComercial + "</option>";
                });
                $("#lstProveedor").html(items);
            });
        }
        else if (this.value == 'Sucursal') {
        // ... and so on, declaring an "items" in each scope where you need it

关于javascript - 删除客户端的最后一个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44119250/

相关文章:

javascript - 使用 three.js 多次显示同一个 3D 对象

Javascript - 归约和数组

javascript - Google Sheets API 与 Ionic

javascript - 文本颜色根据背景图像的颜色变化

javascript - jquery移动: Uncaught TypeError: Cannot read property 'abort' of undefined error in ajax call

javascript - 在 jQuery 集合上使用 `push`

jquery - 流程图 : graph doesn't reflect changes to data

javascript - 如果我不使用 cognito,如何使用 Nodejs 在 axios 中准确签署 AWS elasticsearch 搜索请求?

javascript - 如何使用 JavaScript 对两个变量进行加、减或乘?

c# - 验证驾驶执照号码?