首页 > 解决方案 > 选中 razorview 表中的所有复选框

问题描述

我想在单击标题复选框时选择表中的所有复选框。我不知道该怎么做。这是我到目前为止所做的。

    <table class="table" id="myTable">
        <thead>
            <tr>
                <th>
                    <input type="checkbox" id="selectAll" />
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.SNumber)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Description)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Quantity)
                </th>
                <th></th>
            </tr>
        </thead>
        @foreach (var item in Model)
        {
            @Html.HiddenFor(model => item.PartNumber)
            <tr>
                <td>
                    @Html.CheckBoxFor(modelItem => item.Transfer)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.SNumber)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Description)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Quantity)
                </td>
                <td>
                    @Html.ActionLink("Edit", "EditDetails", new { id = item.PartNumber }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.PartNumber }, new { onclick = "return confirm('Are sure wants to delete?');" })
                </td>
            </tr>

        }
<script>
    $('#selectAll').click(function (e) {
        $(this).closest('table').find('td input:checkbox').prop('checked', this.checked);
    });
</script>

这是控制台返回的内容:

未捕获的 ReferenceError:$ 未定义

我该如何克服它?我是 MVC 的新手。任何和所有的帮助将不胜感激。

标签: javascriptjqueryasp.net-mvcrazor

解决方案


这个错误

未捕获的 ReferenceError:$ 未定义

表示不包括 jQuery。首先,检查您是否已正确包含在您的BundleConfig.cs文件中,如下所示

    public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        // ... more code
        // This is the line that include jQuery
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));
        // ... more code

    }
}

您必须确认您的Script文件夹中确实有一个名为jquery-versionnumber.js的文件。versionnumber可以是您想要使用的任何版本。然后,验证您的视图中的某处(默认情况下在您的_Layout.cshtml中)您已包含脚本,如下所示

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

注意~/bundles/jquery字符串在cs文件和cshtml视图中是相同的。在浏览器中重新运行并验证 jQuery 文件是否实际包含在内。


推荐阅读