gpt4 book ai didi

c# - 如何从下拉列表中获取 id?

转载 作者:行者123 更新时间:2023-11-30 22:09:53 24 4
gpt4 key购买 nike

我有一个列表:

public IList<SelectListItem> ToCountryList { get; set; }

我在 Razor View 中有这一行:

@Html.DropDownListFor(m => m.Filter.ToCountryId, Model.ToCountryList, string.Empty, new { style = "min-width: 100px; width:180px" })

现在我希望 jQuery 从下拉列表的选定值中选择 ToCountryId
我需要记录的ID。例如 - 我有一对:ID-NAME 5-USA。所以我需要检索 5 (int)

如何获取id?

这什么也没给出:

$("select[name='Filter.ToCountryId'] option:selected").val()

来自浏览器的 HTML(不确定如何发布 - 所以纯文本):

..select starts

select id="Filter_ToCountryId" name="Filter.ToCountryId" style="min-width: 100px; width:180px"

option tags with countries

..select ends

最佳答案

只需给您的下拉列表一个确定性的 id:

@Html.DropDownListFor(
m => m.Filter.ToCountryId,
Model.ToCountryList,
string.Empty,
new { id = "myddl", style = "min-width: 100px; width:180px" }
)

然后您可以使用 id 选择器轻松获取所选值:

$('#myddl').val()

关于c# - 如何从下拉列表中获取 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21258356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com