简体   繁体   English

如何获取选中的单选按钮的值

[英]How to get the value of checked radio button

I am trying to make a form that is reactive to what a user inputs.我正在尝试制作一种对用户输入的内容有反应的表单。 I have a list of radio buttons, and for now, I just want to log the value of whatever button is selected in the console (I will implement more logic later).我有一个单选按钮列表,现在,我只想记录在控制台中选择的任何按钮的值(稍后我将实现更多逻辑)。 I know I will need some sort of event listener to log the value if a user changes their selection as well.我知道如果用户也更改了他们的选择,我将需要某种事件侦听器来记录值。

I am still learning so any explanation with answers is greatly appreciated.我仍在学习,因此非常感谢任何带答案的解释。

Here is an example of what I have now.这是我现在拥有的一个例子。

<input type='radio' value='value1' name='example' id='value1'>
<label for='value1'>Value 1</label>
<input type='radio' value='value2' name='example' id='value2'>
<label for='value2'>Value 2</label>
<input type='radio' value='value3' name='example' id='value3'>
<label for='value3'>Value 3</label>
let valueName = document.querySelector('input[name="example"]:checked').value;

When I log valueName to the console.当我将 valueName 记录到控制台时。 I get a TypeError: Cannot read properties of null (reading 'value'.我收到 TypeError: Cannot read properties of null (reading 'value'.

Im not sure where to go from here.我不确定从这里去哪里。

To get an immediate value (ie: on DOM ready) you'll need a checked attribute on one of your radio INPUTs.要获得即时值(即:在 DOM 就绪时),您需要在其中一个无线电输入上checked属性。
Then to get the value on change use addEventListener() like ie:然后使用addEventListener()获取更改值,例如:

 const getValue = () => document.querySelector('input[name="example"]:checked').value; console.log(getValue()) // Do something on change: document.querySelectorAll('input[name="example"]').forEach(elExample => { elExample.addEventListener("input", () => { console.log(getValue()) }); });
 <label><input type='radio' value='value1' name='example' checked> Value 1</label> <label><input type='radio' value='value2' name='example'> Value 2</label> <label><input type='radio' value='value3' name='example'> Value 3</label>

The reason for your error is because the selector you have specified IS NULL at the point that the code runs.错误的原因是因为您在代码运行时指定的选择器为 NULL。

If you're doing document.querySelector('input[name="example"]:checked').value onload, then the selector doesn't exist because there is no input with name="example" which has the checked attribute.如果您正在执行document.querySelector('input[name="example"]:checked').value onload,则选择器不存在,因为没有具有name="example"且具有checked属性的input There is however three input elements with name="example" but that isn't what you're telling the JavaScript to select.然而,有name="example"的三个input元素,但这不是您要告诉 JavaScript 选择的内容。

To make this function as you intend, then you need to use an event listener as you correctly assumed in your question.要按照您的意愿实现此功能,您需要使用您在问题中正确假设的事件侦听器。

 // Select all inputs with name="example" var radios = document.querySelectorAll("input[name=\"example\"]"); // Use Array.forEach to add an event listener to each radio element. radios.forEach(function(radio) { radio.addEventListener('change', function() { let valueName = document.querySelector('input[name="example"]:checked').value; console.log(valueName) }) });
 <input type='radio' value='value1' name='example' id='value1'> <label for='value1'>Value 1</label> <input type='radio' value='value2' name='example' id='value2'> <label for='value2'>Value 2</label> <input type='radio' value='value3' name='example' id='value3'> <label for='value3'>Value 3</label>

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

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