【问题标题】:('...').each not a valid function for asp.net core mvc with jquery bundle('...').each 不是带有 jquery 包的 asp.net core mvc 的有效函数
【发布时间】:2017-08-16 16:54:11
【问题描述】:

重新发布此内容是因为我找不到任何不涉及使用 Google 捆绑包或其他东西的答案。请告诉我是否遗漏任何细节,以便我补充。我的 _Layout 顶部引用了 VS 中包含的整个 jquery 包...

@Scripts.Render("~/bundles/jquery")

这是 BundleConfig.cs

public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/bootstrap.js",
                      "~/Scripts/respond.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css"));
        }

...但在这种情况下它仍然告诉我 .each 不是一个函数。我缺少什么或需要参考布局或 BundleConfig?为什么 .each 不是 Visual Studio 2017 附带的 jquery 包的一部分?

@Html.ListBox("Groups", null, new { size = 10, style = "width : 350px; max-width : 500px;", id = "grouplist" })

我的 jquery 的整个上下文:

    $('#savegroups').click(function () {
            var groupId = $('#groupId').val();
            var list = [];
            ('#grouplist').each(function (item) {
                list.append('{"groupId":"' + groupId + '","groupName":"' + item.value +'"}');
            });
            $.ajax({
                type: "POST",
                url: "/GroupCategories/EditGroups",
                data: JSON.stringify(list),
            });
   });

错误:

1:230 Uncaught TypeError: "#grouplist".each is not a function
    at HTMLInputElement.<anonymous> (1:230)
    at HTMLInputElement.dispatch (jquery:1)
    at HTMLInputElement.y.handle (jquery:1)

【问题讨论】:

  • ('#grouplist') 更改为 $('#grouplist')
  • 我试过了,它只是跳过了整个循环。

标签: c# jquery asp.net-core-mvc visual-studio-2017


【解决方案1】:

看起来您缺少 jquery 选择器..

('#grouplist')

应该是

$('#grouplist option')

【讨论】:

  • 添加美元符号选择器后为什么会完全跳过循环?
  • 更新了我的答案,添加“选项”以循环遍历项目
  • 我认为它正在工作,但事实并非如此,即使添加选项它仍然会忽略循环,它会转到每一行然后跳过循环,尽管列表框中有项目。我将发布一个单独的问题,因为我认为这是一个不同的问题。
猜你喜欢
  • 2017-02-13
  • 1970-01-01
  • 2016-08-06
  • 1970-01-01
  • 2011-03-01
  • 2018-06-29
  • 2018-05-21
  • 1970-01-01
  • 2018-08-24
相关资源
最近更新 更多