简体   繁体   English

如何使下拉菜单默认为空白

[英]How to make drop-down default to blank

When I go to my web page, it has a drop down list with a name already selected.当我 go 到我的 web 页面时,它有一个下拉列表,其中已经选择了一个名称。 I want that it to default to a blank value every time someone goes to that page.我希望每次有人访问该页面时它都默认为空白值。 They will need to type in that blank space.他们将需要输入该空白区域。

<select asp-for="OwnerId" class="form-control" asp-items="@Model.OwnerList"></select>
<select asp-for="OwnerId" class="form-control" asp-items="@Model.OwnerList"></select>

The way i found to have that empty space in the drop down show first was this:我发现首先在下拉显示中有那个空白空间的方式是:

<select asp-for="OwnerId" class="form-control" asp-items="@Model.OwnerList">
    <option value=""></option>
</select>

Thanks for the help.谢谢您的帮助。 This was my first post and got help almost immediately, This is html.这是我的第一篇文章,几乎立即得到了帮助,这是 html。 next time i will know more about how to post properly.下次我会更多地了解如何正确发布。

1) Make sure Model.OwnerList first value is an empty value (so it is the default value for the list). 1)确保Model.OwnerList第一个值为空值(因此它是列表的默认值)。

Heres an example of creating an empty SelectListItem to place as your first item in Model.OwnerList下面是创建一个空的SelectListItem以作为您在Model.OwnerList中的第一个项目的示例

var empty = new SelectListItem() {
    Text = "",
    Value = ""
};

2) Make sure OwnerId doesn't already contain a value 2) 确保OwnerId尚未包含值

If OwnerId already has a value, it will select the first item it finds in Model.OwnerList that matches that value如果OwnerId已经有一个值,它将 select 在Model.OwnerList中找到与该值匹配的第一个项目

Do these two things and you will have an empty selection on load做这两件事,你会在加载时有一个空的选择

Use append/prepend or appendTo/prependTo to add new empty option to the beginning or end of the list with jquery.使用 append/prepend 或 appendTo/prependTo 将新的空选项添加到 jquery 列表的开头或结尾。

Make sure to run the script after document loaded:确保在加载文档后运行脚本:

$(function () {
    $("#OwnerId").prepend('<option></option>').val('');
});

or或者

$(function () {
    $('<option>', { value: '' }).appendTo($("#OwnerId"));
});

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

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