用ajax调用动态填充列表的显示值后刷新 - javascript

我有两个下拉列表-类别,子类别。当我选择一个类别时,子类别列表将基于该类别进行更新。类别ID是通过ajax调用发送的,并返回所选的子类别。我也在使用基础,但是我一直在测试没有基础的所有内容,并且本质上是相同的。

最初,在页面加载时,子类别将正确更新。当我更改为其他类别时,子类别将正确更新,但子类别列表上的所选文本将与先前选择的值相同。有没有办法刷新列表上显示的文本?或者我在JavaScript中做错了什么?同样,即使没有子类别,子类别的选定值文本也将是先前的文本。我尝试在控制器操作中添加默认值服务器端,以查看它是否默认为默认值,但事实并非如此。

我的HTML:

Subcategory: @Html.DropDownListFor(m => m.SubcategoryId, Subcategorylist,

"--Select--", new { id = "Subcategory"})

我的Javascript:

$(document).ready(function () {
    $("#Category").on("change", function () {
        debugger;       
        ChangeSubCategories(this.value);
        $(document).foundation();
    });
    ChangeSubCategories($("#Category").val());
    $(document).foundation();

    function ChangeSubCategories(categoryID) {
        debugger;
        $.ajax({
            type: "POST",
            url: "@Url.Action(MVC.Admin.GetSubCategories())",
            data: { categoryID: categoryID },
            success: function (data) {
                $('#Subcategory').empty();
                var options = $("#Subcategory");
                $.each(data, function () {
                    options.append($("<option />").val(this.Id).text(this.name));
                });
                $(document).foundation();
            },
        });

    }

});

控制者

 [HttpPost]
        public virtual JsonResult GetSubCategories(int categoryID)
        {
            using (MultimediaEntities context = new MultimediaEntities())
            {
                var subcategories = context.Subcategories.Where(x => x.parentCategoryId == categoryID).ToList();
                Subcategory Default = new Subcategory { Id = 0, isActive = false, parentCategoryId = 0, name = "Choose Subcategory" };
                subcategories.Insert(0, Default);
                return Json(subcategories, JsonRequestBehavior.AllowGet);
            }
        }

控制器方法返回JsonResult,其中包含基于Category List的选定值的项目列表。

参考方案

在Ajax.success方法中,您需要做的就是先添加一个空白选项,然后再添加从ajax调用中获得的选项。

所以基本上是这样的

jsFiddle:https://jsfiddle.net/y77appb0/

jQuery的

$(function() {
  var array = [{
    name: "red",
    id: 1
  }, {
    name: "green",
    id: 2
  }, {
    name: "blue",
    id: 3
  }];

    // Append a please select at the start
  $('.colours').append($("<option />").val("").text("Please select"));
  // And if you really want to set the value do this
  $('.colours').val("");
  $.each(array, function(key, value) {
    $('.colours').append(
      $("<option />").val(value.id).text(value.name)
    );
  });
});

javascript popupwindow之后的行如何工作? - javascript

好的,我有一个来自后面代码的方法,可以创建一个popupwindow。然后有一行代码要在那之后执行,我想知道那行代码何时执行,是在使用popupwindow之后执行还是在创建popupwindow之后执行?例如:void exPopupWindowMethod() { string scr = "window.open('exampleP…

ddl在服务器中未更新-asp.net - javascript

我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……

.NET C#Webbrowser填充输入,不带ID或类名 - javascript

我需要在网络浏览器中填写一个输入,但这不起作用。我认为必须使用name属性来完成,但是怎么做呢?foreach (HtmlElement login in webBrowser1.Document.GetElementsByTagName("input")) { if (login.GetAttribute("name"…

Javascript-Urls的奇怪字符串比较行为 - javascript

最近,在编写我无法理解的javascript时遇到了字符串比较的问题。我从完全相同的网址创建了两个字符串,当我比较它们时返回false,但是在重新分配相同的字符串后,比较返回true。这是我的示例:var str1 = "http://google.com/"; var str2 = "http://google.com‏/&#…

从控制器以视图(javascript)访问会话 - javascript

这是我的控制器代码。我想获取视图中存储在会话中的值(JavaScript代码) decimal.TryParse(permotion.PROMOTION_AMOUNT.ToString(), out promotionAmount); int.TryParse(permotion.PROMOTION_TYPE_ID.ToString(CultureInfo.…