简体   繁体   English

使用jQuery禁用asp.net mvc下拉列表

[英]disable asp.net mvc dropdown using jquery

Below is my jquery code and my ASP>NET mvc razor code. 以下是我的jquery代码和ASP> NET mvc剃刀代码。 Razor code is for dropdown list. 剃刀代码用于下拉列表。 I want to disable the dropdown when the labelCount variables value is 0. The values comes to 0 but the dropdown is not getting disabled. 当labelCount变量的值为0时,我想禁用下拉列表。值变为0,但下拉列表没有被禁用。

if (labelCount == 0) {
    $("#AndOrSelection").attr("disabled", "disabled");
}

@Html.DropDownList("AndOrSelection", new List<SelectListItem>()
{ 
    new SelectListItem { Text = "And", Value = "And" },
    new SelectListItem { Text = "Or", Value = "Or" }
}, "Select criteria")

The best place to put your jQuery code is near the closing body tag (as best practices recommend) 放置jQuery代码的最佳位置是靠近body标记(按最佳做法推荐)

Following will work 以下将起作用

 @Html.DropDownList("AndOrSelection", new List<SelectListItem>()
 { 
   new SelectListItem { Text = "And", Value = "And" },
   new SelectListItem { Text = "Or", Value = "Or" }
 }, "Select criteria")

<script>
if (labelCount == 0) {
$("#AndOrSelection").attr("disabled", "disabled");
}
</script>
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM