Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 在change函数中如何访问unique元素?_Jquery_Html_Razor - Fatal编程技术网

Jquery 在change函数中如何访问unique元素?

Jquery 在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

@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