2012-03-03 92 views
19

我的表单中有一些选项按钮,我总是需要完全点击圆选项按钮才能选中它。有没有办法让用户点击相关的标签来选择选项按钮?如何将标签元素与单选按钮相关联

enter image description here

<p> 
    <span class="editor-label"> 
     @Html.LabelFor(m => m.ADR) 
    </span> 
    <span class="editor-field"> 
     @Html.RadioButtonFor(model => model.ADR, "Yes", AdrYesOptions) 
     @UserResource.YesValue 
     @Html.RadioButtonFor(model => model.ADR, "No", AdrNoOptions) 
     @UserResource.NoValue 

    </span> 
</p> 

我发现了一些解决方案,在这里:How to associate labels with radio buttons但这些不适合我,因为我更喜欢特定于MVC的解决方案。

+0

你是什么意思_specific为MVC_是什么意思?链接的问题包含一个有效的答案(尽管不是公认的答案),说明如何实现这一点。 – marapet 2012-03-03 11:53:31

+0

我的意思是(如果可能)保持我的代码Html.LabelFor ... Html.RadioButtonFor ...而不是像

回答

40

<label>元素中每个单选按钮和相关联的标签文字:

<p> 
    <span class="editor-label"> @Html.LabelFor(m => m.ADR) </span> 
    <span class="editor-field"> 
     <label> @Html.RadioButtonFor(model => model.ADR, "Yes", AdrYesOptions) @UserResource.YesValue </label> 
     <label> @Html.RadioButtonFor(model => model.ADR, "No", AdrNoOptions) @UserResource.NoValue </label> 
    </span> 
</p> 
+1

请注意,如果用户单击主标签' @ Html.LabelFor(m => m.ADR)'这将选择第一个无线电选项。 – 2014-10-28 17:22:14