0

我的 Html 代码如下。但始终将最终选项按钮选中为选中的。我该如何解决这个问题?

<div id="employee" style="margin: 7px 0;">
            <div>
                   <input id="james-johnson" type="radio" name="employee" value="james-johnson" checked="checked"><label for="james-johnson">James Johnson</label>
               </div>
            <div>
                   <input id="boss-hog" type="radio" name="employee" value="boss-hog" checked="unchecked"><label for="boss-hog">Boss Hog</label>
               </div>
            <div>
                   <input id="jake-sanders" type="radio" name="employee" value="jake-sanders" checked="unchecked"><label for="jake-sanders">Jake Sanders</label>
               </div>
            <div>
                   <input id="dawn" type="radio" name="employee" value="dawn" checked="unchecked"><label for="dawn">Dawn</label>
               </div>

        </div>

ASPX 代码如下

<div id="employee" style="margin: 7px 0;">
            <% foreach (var employee in Model.Provider.Employees)
               {  %><div>
                   <input id="<%: employee.Key %>"
                       type="radio" name="employee" value="<%: employee.Key %>" checked="<%:employee.IsEmployeeSelected ?? "unchecked" %>" /><label for="<%: employee.Key  %>"><%: employee.Name%></label>
               </div>
            <% } %>
        </div>

更新(当我应用 @Satpal 方法时):

<div id="employee" style="margin: 7px 0;">
            <div>
                   <input id="james-johnson" type="radio" name="employee" value="james-johnson" checked="checked"><label for="james-johnson">James Johnson</label>
               </div>
            <div>
                   <input id="boss-hog" type="radio" name="employee" value="boss-hog" checked=""><label for="boss-hog">Boss Hog</label>
               </div>
            <div>
                   <input id="jake-sanders" type="radio" name="employee" value="jake-sanders" checked=""><label for="jake-sanders">Jake Sanders</label>
               </div>
            <div>
                   <input id="dawn" type="radio" name="employee" value="dawn" checked=""><label for="dawn">Dawn</label>
               </div>

        </div>
4

1 回答 1

1

As 选中的属性是一个布尔属性。如果存在,它指定在页面加载时应该预先选择(检查)一个元素。

仅在不设置checked属性时才设置属性employee is Selected

<input id="<%: employee.Key %>"
   type="radio" name="employee" value="<%: employee.Key %>" 
   "<%:employee.IsEmployeeSelected ? "checked" : "" %>" />
   <label for="<%: employee.Key  %>"><%: employee.Name%>
   </label>

修改后的代码

"<%: employee.IsEmployeeSelected ? "checked" : "" %>" 

转换employee.IsEmployeeSelected为布尔属性

编辑:根据评论目前它是“检查”并且为空

"<%: !string.IsNullOrEmpty(employee.IsEmployeeSelected) ? employee.IsEmployeeSelected : "" %>"

或者简单地设置<%:employee.IsEmployeeSelected ?? "" %>

<input <%:employee.IsEmployeeSelected ?? "" %> />

更新

由于 OP 已将该字段更新IsEmployeeSelected为布尔值

 public bool IsEmployeeSelected { get; set; }

因此使用<%:employee.IsEmployeeSelected ? "checked" : "" %>而不是checked="<%:employee.IsEmployeeSelected ? "checked" : "" %>"

于 2013-10-08T15:08:57.030 回答