javascript - 选中 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 的新手。任何和所有的帮助将不胜感激。
解决方案
这个错误
未捕获的 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 文件是否实际包含在内。
推荐阅读
- linux - 仅提取 url 段而不是 shell 脚本中的以下正则表达式?
- bash - 在 kafka-connect docker 启动并运行后运行 bash 脚本
- c++ - 即使使用了...,参数包也不会用“...”扩展
- javascript - ThreeJS:在 PlaneBufferGeometry 中查找邻居面
- python - 即使我的语法正确,为什么我在 opencv 中的 rectangle() 命令中出现错误
- google-cloud-tasks - 删除 Google Cloud Tasks 时可以使用通配符吗?
- excel-dna - 将 Excel RTD 库转换为 Excel-DNA,同时保留 =RTD( 接口(64 位 Excel)
- postgresql - 如何在 PostgreSQL 中表示评级?
- java - Cloudera 向导步骤“安装代理”期间的 SSH 问题
- powershell - 使用 PowerShell 触发 Dropbox api saveurl get 400 错误