Jquery 在change函数中如何访问unique元素?
将剃刀与模型一起使用。阅读脚本中的注释 HTMLJquery 在change函数中如何访问unique元素?,jquery,html,razor,Jquery,Html,Razor,将剃刀与模型一起使用。阅读脚本中的注释 HTML @Html.DropDownListFor(model => model.List[i].information, Model.ListDropdown(), new { @id = "GetList" + i}) 脚本 $("[id^=GetList]").change(function () { // How can I get the 'i' value after the id? } 谢谢您可以替换元素的id属性中的G
@Html.DropDownListFor(model => model.List[i].information, Model.ListDropdown(), new { @id = "GetList" + i})
脚本
$("[id^=GetList]").change(function () {
// How can I get the 'i' value after the id?
}
谢谢您可以
替换元素的id
属性中的GetList
。然而,一种更简单的方法是使用数据
属性。此外,我还添加了一个class
属性,以避免您不得不使用(相当慢的)'attributestartswith'选择器
@Html.DropDownListFor(
model => model.List[i].information,
Model.ListDropdown(),
new { @class = "list-item", data_id = i }
)
然后,您可以直接在change
处理程序中检索此值:
$(".list-item").change(function () {
var id = $(this).data('id');
}
请注意,change
功能块中的this
指引发事件的select