所以在我的ASP.NET MVC应用程序中,我有一个带有多个值的下拉列表的视图。基本上我希望能够为每个选定的值调用不同的JS函数。
型号:
[Display(Name = "Rate")]
public string Rate { get; set; }
以下是我的观点:
List<SelectListItem> rate = new List<SelectListItem>();
rate.Add(new SelectListItem
{
Text = "1 Rate",
Value = "1",
});
rate.Add(new SelectListItem
{
Text = "2 Rates",
Value = "2"
});
rate.Add(new SelectListItem
{
Text = "3 Rates",
Value = "3",
});
rate.Add(new SelectListItem
{
Text = "4 Rates",
Value = "4"
});
rate.Add(new SelectListItem
{
Text = "5 Rates",
Value = "5"
});
}
<div class="form-group">
@Html.LabelFor(model => model.Rate, htmlAttributes: new { @class = "control-label text-left " })
<div class="">
@Html.DropDownListFor(model => rate,
new SelectList(rate, "Text", "Value"),
"Vælg rate",
new { @class = "form-control", required = "required" })
</div>
</div>
我不确定在哪里添加我的js功能。我试过添加“onchange”或“onclick”之类的东西,但它们不起作用。
答案 0 :(得分:0)
@Html.DropDownListFor(x => x.subitemid, Enumerable.Empty<SelectListItem>(), "Select Sub Category", new { @class = "form-control select2" })
<script type="text/javascript">
$(document).ready(function () {
$('select#subitemid').change(function () {
var itemId = $(this).val();
});
});
</script>
我觉得它对你很有用