如何检查正确的单选按钮
我有一个包含我需要的一些信息的表。所有这些行还包含一个带有单选按钮的列,以便用户能够默认检查其中一行。
当我从数据库取回数据并想要选择当前默认的数据时。
<% 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 html 中检查没有布尔值 true 或 false。
您必须设置checked =“checked”才能选中复选框(如果您想要正确的语法)。但大多数浏览器接受任何checked =“...”作为设置。所以你的checked =“false”被解释为“已检查”。因此,所有复选框都被解释为已选中,因为只有一个复选框可以选中,所以最后一个复选框会被选中。
如果你不想让它被检查,你必须删除整个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.