简体   繁体   English

使用 HTML 单选按钮的正确方法?

[英]Correct way to use HTML radiobutton?

I have 3 radiobuttons.我有 3 个单选按钮。 I want they act like radiobuttons : if the user click one, the other can't be clicked.我希望它们像单选按钮一样:如果用户单击一个,则无法单击另一个。 But somehow my buttons are acting like checkboxes, the user can select all 3. Here is my code, what am i missing ?但不知何故,我的按钮就像复选框一样,用户可以选择所有 3 个。这是我的代码,我错过了什么? Thanks !谢谢 !

<div class="form-group">
<div class="form-item" id="form-item-hi_status1">
<label for="input-status1" ></label>
<input class="radio-input" type="radio" id="input-hi_status1" name="input-hi_status1" data-label="Option 1" data-mask="radiobutton" value="1">
<label class="radio-label">Option 1</label>
</div>
<div class="form-item" id="form-item-hi_status2">
<label for="input-status2" ></label>
<input class="radio-input" type="radio" id="input-hi_status2" name="input-hi_status2" data-label="Option 2" data-mask="radiobutton" value="2">
<label class="radio-label">Option 2</label></div>
<div class="form-item" id="form-item-hi_status3">
<label for="input-status3" ></label>
<input class="radio-input" type="radio" id="input-hi_status3" name="input-hi_status3" data-label="Option 3" data-mask="radiobutton" value="3"><label class="radio-label">Option 3</label>
</div>

Here is an image showing the problem.这是显示问题的图像。 I DON'T USE W3Schools as a learning resource, i just used their HTML editor to show the example.我不使用 W3Schools 作为学习资源,我只是使用他们的 HTML 编辑器来展示示例。

例子

All the radio buttons in a group must have the same value for the name attribute.组中的所有单选按钮的name属性必须具有相同的值。 That's what makes them mutually exclusive.这就是使它们相互排斥的原因。

Here's the documentation for <input type='radio'> which includes:这是<input type='radio'> 的文档,其中包括:

The "name" setting is an important attribute of radio buttons, as it identifies which group a radio button belongs to. “名称”设置是单选按钮的一个重要属性,因为它标识了单选按钮属于哪个组。 Because groups of radio buttons act as a unit, you must specify a common name for all related radio buttons.由于单选按钮组作为一个单元,您必须为所有相关的单选按钮指定一个通用名称。 When two or more radio buttons share a name, selecting one of the buttons will unselect all of the others with the same name.当两个或多个单选按钮共享一个名称时,选择其中一个按钮将取消选择所有具有相同名称的其他按钮。 If you have more than one group of radio buttons on a single page, the buttons in different groups must have different "name" attributes.如果在一个页面上有多个单选按钮组,则不同组中的按钮必须具有不同的“名称”属性。

Radio buttons are grouped together using the name attribute.单选按钮使用name属性组合在一起。 give them the same name .给它们起相同的name

<div class="form-group">
<div class="form-item" id="form-item-hi_status1">
   <label for="input-status1" ></label>
   <input class="radio-input" type="radio" id="input-hi_status1" name="input-hi_status" data-label="Option 1" data-mask="radiobutton" value="1">
   <label class="radio-label">Option 1</label>
</div>
<div class="form-item" id="form-item-hi_status2">
   <label for="input-status2" ></label>
   <input class="radio-input" type="radio" id="input-hi_status2" name="input-hi_status" data-label="Option 2" data-mask="radiobutton" value="2">
   <label class="radio-label">Option 2</label>
</div>
<div class="form-item" id="form-item-hi_status3">
   <label for="input-status3" ></label>
   <input class="radio-input" type="radio" id="input-hi_status3" name="input-hi_status" data-label="Option 3" data-mask="radiobutton" value="3">
   <label class="radio-label">Option 3</label>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM