【问题标题】:How to disable @html.dropdownlist option label in .NET?如何在 .NET 中禁用 @html.dropdownlist 选项标签?
【发布时间】:2022-12-16 20:51:11
【问题描述】:
查看标记:
@Html.DropDownListFor(model => model.entityType, ViewBag.entityType as SelectList, "Select Type", new { @class = "custom-select"})
这是.NET 中的下拉列表,“选择类型”是选项标签。
我的期望:我想禁用“选择类型”选项标签。我怎样才能做到这一点 ?
提前感谢谁帮助我。
问候:
凤凰
【问题讨论】:
标签:
c#
asp.net
.net
asp.net-mvc
asp.net-core
【解决方案1】:
可选标签具有空的 value 属性。因此,下面的 JavaScript 使其成为disabled:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
document.querySelectorAll("#entityType option").forEach(opt => {
if (opt.value == "") {
opt.disabled = true;
}
});
});
</script>
@Html.DropDownListFor(model => model.entityType, ViewBag.entityType as SelectList, "Select Type", new { @class = "custom-select"})