如何检查正确的单选按钮

发布于 2024-09-02 11:18:23 字数 733 浏览 1 评论 0原文

我有一个包含我需要的一些信息的表。所有这些行还包含一个带有单选按钮的列,以便用户能够默认检查其中一行。

当我从数据库取回数据并想要选择当前默认的数据时。

<% 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>

我也尝试过这样做:

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

但似乎没有什么是正确的。我总是最终检查最后一行,但这是不确定的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哭了丶谁疼 2024-09-09 11:18:23

在 html 中检查没有布尔值 true 或 false。
您必须设置checked =“checked”才能选中复选框(如果您想要正确的语法)。但大多数浏览器接受任何checked =“...”作为设置。所以你的checked =“false”被解释为“已检查”。因此,所有复选框都被解释为已选中,因为只有一个复选框可以选中,所以最后一个复选框会被选中。

如果你不想让它被检查,你必须删除整个checked=属性。

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

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\"" %> />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文