开发者

How to check the correct radio button

I have a Table containing some information that I need. All these rows also contains a column with a radio button in it so that the user is suppose to be able to check one of the rows as default.

When I'm bringing the data back from the DB and want to select the one that's currentlly the default one.

<% foreach (var item in (IEnumerable<Locale>) ViewData["Locales"]) { %>
    <tr>
        <td>
            <%= Html.Encode(item.Language.Name) %>
        </td>
        <td>
            <input type="radio" id="defaultLocale" name="defaultLocele" value="on" checked="<%= item.Default == false ? "false" : "true" %>" />
        <开发者_开发问答/td>

I've also tried to do this:

<input type="radio" id="defaultLocale" name="defaultLocele" value="on" checked="<%=item.Default == false ? "" : "checked" %>" />

but nothing seems to do the right thing. I always end up with having the last row in checked, which it isn't for sure.


In html checked is no bool value of true or false. You have to set checked="checked" in order to have the checkbox checked (If you want correct syntax). But most browsers accept any checked="..." as setting. So your checked="false" gets interpreted as "Is checked". So all your checkboxes are interpreted as checked and becouse only one can be, the last one is checked.

If you dont want it to be checked, you have to remove the whole checked= attribute.

<input type="radio" id="defaultLocale" name="defaultLocele" value="on" <%=item.Default ? "" : "checked=\"checked\"" %> />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜