我在.aspx页面中创建了一个选择列表。
<label for="AccessType" class="required"><span class="required">*</span><%=Html.Resource("accessType")%>:</label>
<select id="AccessType" name="AccessType">
<% foreach (var item in Enum.GetValues(typeof(Security.AccessType)))
{%>
<option value="<%=(int)item%>"><%=item%> </option>
<%}%>
</select><br />
现在每次加载页面时,它都会选择第一个值作为默认值,因为我希望模型中存在的值是所选的。
我在我的代码中将下拉列表绑定到枚举。 Security.AccessType是枚举而不是模型。因此,每次加载页面时,它都会将下拉列表的选定值显示为第一个枚举
我希望所选项目可以说是Model.AccessType ...
我知道这是一个非常基本的问题,但仍有任何帮助吗?
答案 0 :(得分:0)
您可以将要作为默认选项的选项指定为“已选择”属性,例如:
<option value="apple" selected="selected">apple</option>
如果要动态选择一个选项,可以通过javascript完成。例如:
var el = document.getElementById("AccessType");
el.selectedIndex = yourIndex; //index of the option you want to select
//or
el.value = "yourValue"; //value of the option you want to select
答案 1 :(得分:0)
我使用了followig并得到了结果..
$('#ddlAccessType')。val($(“#ddlAccessType选项:contains('”$ +('#AccessTypeValue')。val()+“')”)。val());
首先获取AccessType值,然后在下拉列表中检查其索引,然后将所选索引设置为找到的索引。
有点古怪,但对我来说很好..