简体   繁体   English

如何向使用 HTML 帮助程序创建的下拉列表添加其他属性?

[英]How to add additional attributes to a dropdown created using an HTML helper?

I have two dropdowns created using HTML helpers, however i am not entirely sure on where i could add additional attributes to these two helpers?我有两个使用 HTML 助手创建的下拉菜单,但是我不完全确定我可以在哪里向这两个助手添加其他属性? More specifically, i want to add an "onblur" attribute that calls a javascript function, as follows:更具体地说,我想添加一个调用 javascript 函数的“onblur”属性,如下所示:

@onblur = "validate3(1)"

I want to be able to give the following HTML Helper dropdowns the above attribute:我希望能够为以下 HTML Helper 下拉菜单提供上述属性:

@Html.DropDownList("ExpMonth", new List<SelectListItem>
                                {
                                    new SelectListItem { Text = "01", Value = "1"},
                                    new SelectListItem { Text = "02", Value = "2"},
                                    new SelectListItem { Text = "03", Value = "3"},
                                    new SelectListItem { Text = "04", Value = "4"},
                                    new SelectListItem { Text = "05", Value = "5"},
                                    new SelectListItem { Text = "06", Value = "6"},
                                    new SelectListItem { Text = "07", Value = "7"},
                                    new SelectListItem { Text = "08", Value = "8"},
                                    new SelectListItem { Text = "09", Value = "9"},
                                    new SelectListItem { Text = "10", Value = "10"},
                                    new SelectListItem { Text = "11", Value = "11"},
                                    new SelectListItem { Text = "12", Value = "12"}
                                }, "MM")

                                @Html.DropDownList("ExpYear", new List<SelectListItem>
                                {
                                    new SelectListItem { Text = "2020", Value = "1"},
                                    new SelectListItem { Text = "2021", Value = "2"},
                                    new SelectListItem { Text = "2022", Value = "3"},
                                    new SelectListItem { Text = "2023", Value = "4"},
                                    new SelectListItem { Text = "2024", Value = "5"},
                                    new SelectListItem { Text = "2025", Value = "6"},
                                    new SelectListItem { Text = "2026", Value = "7"},
                                    new SelectListItem { Text = "2027", Value = "8"},
                                    new SelectListItem { Text = "2028", Value = "9"},
                                    new SelectListItem { Text = "2029", Value = "10"},
                                    new SelectListItem { Text = "2030", Value = "11"},
                                }, "YY")

Any help is appreciated.任何帮助表示赞赏。

Try尝试

@Html.DropDownList("ExpMonth", new List<SelectListItem>{
    new SelectListItem { Text = "01", Value = "1"},
    new SelectListItem { Text = "02", Value = "2"},
    new SelectListItem { Text = "03", Value = "3"},
    new SelectListItem { Text = "04", Value = "4"},
    new SelectListItem { Text = "05", Value = "5"},
    new SelectListItem { Text = "06", Value = "6"},
    new SelectListItem { Text = "07", Value = "7"},
    new SelectListItem { Text = "08", Value = "8"},
    new SelectListItem { Text = "09", Value = "9"},
    new SelectListItem { Text = "10", Value = "10"},
    new SelectListItem { Text = "11", Value = "11"},
    new SelectListItem { Text = "12", Value = "12"}
}, "MM", new{@onblur = "validate3(1)"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用HTML Helper在创建的TextBox元素中添加新的html属性 - Add new html attribute in the created TextBox element using Html Helper 如何使用 AWS Cognito 添加额外的注册属性? - How to add additional signup attributes using AWS Cognito? 下拉列表中的附加属性 (Javascript) - Additional attributes in a dropdown (Javascript) 如何使用查询将选定的属性传递到 html 中的下拉列表 - How to pass selected attributes to dropdown in html using query 如何使用 javascript 下拉列表将 append 已经创建的 html 表格放入表格中? - How to append already created html form into table using javascript dropdown? 使用HTML Helper DropDownListFor添加选定的标签 - Add selected tag using HTML Helper DropDownListFor 在自定义ActionLink帮助器MVC中使用条件HTML属性 - Using Conditional HTML Attributes in Custom ActionLink Helper MVC 如何使用下拉列表和HTML5数据属性动态更改HTML元素的分组? - How to dynamically change the grouping of HTML elements using a dropdown and HTML5 data attributes? 如何使用 Javascript 将 HTML 属性添加到数组 - How do I add HTML attributes to an array using Javascript 如何通过 javascript 向 IE 中的 XML 添加额外的 xmlns 命名空间属性 - How to add additional xmlns namespace attributes to XML in IE via javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM