Get selectedIndex from @Html.DropDownListFor
Get selectedIndex from @Html.DropDownListFor
我需要一些帮助来获得使用JavaScript的下拉菜单的选定索引。
这是我的Javascript代码:<script type="text/javascript">
$(function () {
$("#test").change(function () {
var index = document.getElementById('test').selectedIndex;
alert(index);
});
});
这是下拉菜单:
<div class="editor-field" id="test">
@Html.DropDownListFor(x => x.TestID, new SelectList(Model.Tests, "TestID", "TestName"))
@Html.ValidationMessageFor(model => model.TestID)
</div>
当改变下拉菜单时,我总是得到"未定义"。有人有解决这个问题的办法吗?
当调用事件处理程序时,jQuery将this
设置为目标元素。然后,您可以使用它来获取目标元素内selected
option
的索引。
$("select", "#test").change(function () {
var index = $("option:selected", $(this)).index()
alert(index);
});
相关文章:
- DropDownListFor赢得't在更改时调用函数
- 为dropdownlistfor(MVC)中的每个元素设置唯一id
- 使用JavaScript更改MVC DropDownListFor值
- 删除DropDownListFor中的项目
- 根据DropDown ID获取DropDownListFor的选定值
- 根据客户端变量的状态有条件地启用/禁用 @Html.DropDownListFor
- ASP.NET MVC 4如何将行添加到包含dropdownlistfor的表中
- dropdownlistfor无法使用jquery更改为控制器中的选定值
- 如何使用DropDownListFor将值传递给函数
- 在用户从DropDownListFor中选择值后禁用TextBoxFor
- 如何填充@Html.当我们从@Html.DropdownListfor中选择一个值时
- @Html.DropDownListFor()总是显示空白,即使列表项被选中
- Get selectedIndex from @Html.DropDownListFor
- 过滤我的@html.dropdownlistfor
- 当在另一个下拉列表中选择特定选项时,删除DropDownListFor选择
- 如何交换2 @Html.Jquery/JS的DropDownListFor
- MVC 4基于DropDownListFor选择更改多个显示字段
- MVC 4基于DropDownListFor选择更改字段