简体   繁体   English

正确使用单选按钮的方法

[英]Right way to use Radiobuttons

I'm confused about which is the right way to use radiobuttons. 我对使用单选按钮的正确方法感到困惑。 Let's say i have a group of 3 radiobuttons : if all of them have the same id / name, it will work properly , only one can be checked : 假设我有一组3个单选按钮:如果它们全部具有相同的ID /名称,它将正常工作,只能检查一个:

<label for="input-rb1" ></label><input class="radio-input" type="radio" id="input-rb1" name="input-rb1"><label for="input-rb1" class="radio-label">First radiobutton</label>
<label for="input-rb1" ></label><input class="radio-input" type="radio" id="input-rb1" name="input-rb1"><label for="input-rb1" class="radio-label">Second radiobutton</label>
<label for="input-rb1" ></label><input class="radio-input" type="radio" id="input-rb1" name="input-rb1"><label for="input-rb1" class="radio-label">Third radiobutton</label>

The problem about this approach is i don't know how to identify which one is checked, because all of them have the same name / id. 关于此方法的问题是我不知道如何识别已检查的方法,因为它们所有人都具有相同的名称/ ID。

The other way would use different names / ids : 另一种方法将使用不同的名称/ ID:

<label for="input-rb1" ></label><input class="radio-input" type="radio" id="input-rb1" name="input-rb1"><label for="input-rb1" class="radio-label">First radiobutton</label>
<label for="input-rb2" ></label><input class="radio-input" type="radio" id="input-rb1" name="input-rb2"><label for="input-rb2" class="radio-label">Second radiobutton</label>
<label for="input-rb3" ></label><input class="radio-input" type="radio" id="input-rb1" name="input-rb3"><label for="input-rb3" class="radio-label">Third radiobutton</label>

This way i know how to use JQuery to know which is checked, but it will allow the user to select multiple radios, like it was checkboxes... 这样我就知道如何使用JQuery来知道选中哪个,但是它将允许用户选择多个单选,就像它是复选框一样。

How to solve this puzzle ? 如何解决这个难题?

Thanks ! 谢谢 !

Only name of radio buttons should be same, id is an unique attribute in elements. 只有单选按钮的名称应该相同,id是元素中的唯一属性。 You should use different ids and also you can use value attribute to assign values. 您应该使用不同的ID,也可以使用value属性分配值。 Code is given below 代码如下

<form action="">
  <input type="radio" id="1" name="gender" value="male"> Male<br>
  <input type="radio" id="2" name="gender" value="female"> Female<br>
</form>

You can use jQuery to get selected radio button value as follow 您可以使用jQuery获取选定的单选按钮值,如下所示

 $("input[type='radio']").on('change', function () {
         var selectedValue = $("input[name='gender']:checked").val();
         if (selectedValue) {
               alert(selectedValue);
           }
      });

An id is a keyword that can be used to find a specific element on a page. id是可用于在页面上查找特定元素的关键字。

<input type="radio" id="radio1">one
<input type="radio" id="radio2">two

  <input type="radio" id="radio1">one <input type="radio" id="radio2">two 

You'll notice the above snippet allows you to select both radio buttons. 您会注意到上面的片段允许您选择两个单选按钮。 There's nothing joining them together and the Browser can't assume they're connected. 没有什么可以将它们连接在一起,并且浏览器无法假定它们已连接。

A name is used to determine a value from an input field - or in the case of radio buttons - a single value from multiple input fields. name用于确定输入字段中的值- 如果是单选按钮 ,则使用多个输入字段中的单个值。 It joins the radio buttons together to say "Hey, only allow one of you to be checked! That's the value we're going with!" 它将单选按钮连接在一起,说:“嘿,只允许检查您一个人!这就是我们要使用的价值!”

<input type="radio" name="radio_group">one
<input type="radio" name="radio_group">two

  <input type="radio" name="radio_group">one <input type="radio" name="radio_group">two 

Notice that there are no ids in the above code. 请注意,以上代码中没有ID。 id and name are not interchangeable. idname不可互换。 They are completely separate attributes aimed to do two completely different things. 它们是完全独立的属性,旨在完成两个完全不同的事情。

The other answers use JQuery and that's all well and good, but I think it's important to give a vanilla JS example as well. 其他答案都使用JQuery,这一切都很好,但是我想举一个普通的JS示例也很重要。

document.querySelectorAll("input[name='r_group']").forEach((radio) => {
    if (radio.checked) {
      //do whatever
    }
});

 document.querySelectorAll("input[name='r_group']").forEach((radio) => { radio.addEventListener("change", () => { if (radio.checked) alert(radio.value); }); }); 
 <input id="radio1" type="radio" name="r_group" value="one">one <input id="radio2" type="radio" name="r_group" value="two">two <input id="radio3" type="radio" name="r_group" value="three">three 

First, the values for "id" should be unique to each element. 首先,“ id”的值对于每个元素应该是唯一的。 The name attribute can (and should) be the same for all elements in the group. 组中的所有元素的name属性可以(并且应该)相同。

Then, to get the value of the checked radio use: 然后,要获取已检查的无线电的值,请使用:

$('input[name=name_of_your_radiobutton]:checked').val();

The second approach is wrong, name in radio buttons is used to group them in same class, only one of them can be selected. 第二种方法是错误的,单选按钮中的名称用于将它们归为同一类,只能选择其中一个。

Just use the following script- 只需使用以下脚本-

$('input[name="input-rb1"]:checked').val();

Avoid using hyphen - in javascript instead use _ if possible 避免使用连字符-如果可能,请在javascript中使用_

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

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