mvc 6中Dropdownlist的TagHelpers onchange事件
本文关键字:TagHelpers onchange 事件 Dropdownlist mvc | 更新日期: 2023-09-27 18:01:10
我有一个下拉列表,我想将Jquery onchange事件绑定到TagHelpers select标记。下面是我的代码。
<select asp-for="BusinessType"
asp-items="@Model.BusinessTypeCollection">
</select>
如何绑定标记的内联onchange事件。
像这样的东西。
<select asp-for="BusinessType"
asp-items="@Model.BusinessTypeCollection"
onchange ="something">
</select>
onchange
是要内联指定的正确属性。您只需要确保(a(正在调用它,(b(该函数在全局范围内可用。
例如:
<select asp-for="BusinessType"
asp-items="Model.BusinessTypeCollection"
onchange="test()"></select>
@section scripts {
<script>
function test() {
alert('hi');
}
</script>
}
话虽如此,一种更好的方法是在JavaScript中绑定事件(正如您在问题中提到的那样,我在这里使用jQuery(,并仅通过元素的id
属性引用该元素。
<select asp-for="BusinessType"
asp-items="Model.BusinessTypeCollection"></select>
@section scripts {
<script>
$("#BusinessType").on("change", function () {
alert("changed!");
});
</script>
}