简体   繁体   English

如何使selectList显示默认值作为jquery中的选中项

[英]How to make the selectList to show the default value as the selected item in jquery

I'm implementing asp.net core 3.1 project.我正在实施 asp.net 核心 3.1 项目。 I have a dropdownlist like the following that gets its data via ViewData in Controller.我有一个如下所示的下拉列表,它通过 Controller 中的 ViewData 获取其数据。

  <div id="displaySelectList" style="display:none;">
                                @Html.DropDownList("reason", new SelectList((IEnumerable<SelectListItem>)ViewData["reason"], "Value", "Text"), "select", new { @class = "form-control", id = "reason" })
                                @Html.ValidationMessage("reason", "select", new { @class = "text-danger" })
                            </div>

The first item is the default item of the DropDownList.第一项是 DropDownList 的默认项。 Now the problem is after choosing an item from that and after again opening the DropDownList, the user can see the former selected item as the default while I want it to show the "select" item as the default.I appreciate if anyone could suggests me a solution.现在的问题是在从中选择一个项目并再次打开 DropDownList 后,用户可以看到以前选择的项目作为默认值,而我希望它显示“选择”项目作为默认值。如果有人可以建议我,我将不胜感激一个解法。

Use this function to reset the dropdown使用此 function 重置下拉菜单

function resetSelectElement(){
    $('select').prop('selectedIndex', 0);
}

Call this function when you close the modal.关闭模态时调用此 function。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM