如何使用单选按钮标签关联 [英] How to associate labels with radio buttons

查看:187
本文介绍了如何使用单选按钮标签关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC和我有一个简单的单选按钮设置:

I'm using MVC, and I've got a simple radio button setup:

<%=Html.RadioButton("my_flag", True)%><label>Yes</label>
<%=Html.RadioButton("my_flag", False)%><label>No</label>

我唯一缺少的是,你不能点击标签,选择单选按钮。通常你会使用:

The only thing I'm missing is that you can't click the label to select the radio button. Normally you'd use:

<label for="my_flag">

但是,与过去的单选按钮两标签相关联。有没有什么办法标签与正确的单选按钮关联?

but that associates both labels with the last radio button. Is there any way to associate the labels with the correct radio button?

请注意:这是模仿纸张形式,所以切换到一个复选框是不是一种选择

Note: This is mimicking a paper form, so switching to a checkbox is not an option.

推荐答案

您需要给两个单选按钮相同的名称(让他们在同一组),但不同的< STRONG> IDS (这样你可以单独与每一个标签相关联)。我不熟悉ASP.NET MVC,所以我不知道如何真正做到这一点,但是这是解决方案。

You need to give the two radio buttons the same name (so that they're in the same group), but different ids (so that you can associate the label with each one individually). I'm not familiar with ASP.NET MVC, so I don't know how to actually accomplish this, but that's the solution.

编辑:第二个可能性是包单选按钮标签标签内,像这样:

a second possibility is to wrap the radio buttons inside the label tag, like so:

<label><%=Html.RadioButton("my_flag", True)%>Yes</label>
<label><%=Html.RadioButton("my_flag", False)%>No</label>

*请注意,这种方式实际上可能有更好的浏览器兼容性,我知道有些浏览器仍然前途未卜有关名称/ ID区别

这篇关于如何使用单选按钮标签关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆