无法计数JavaScript中的形式值
我得到了一个具有编号值的输入
<input type="radio" id="ja" name="upprepas" value="0">
<input type="radio" id="ja" name="bekanta" value="2">
,但我也有其他10个输入,我想计算所有输入的值。所以我尝试了一下:
var select = document.querySelector('input[name="upprepas"]:checked');
console.log(select.value);
var selecto = document.querySelector('input[name="bekanta"]:checked');
console.log(selecto.value);
var selector = select.value + selecto.value;
console.log(selector.value);
但是选择器总是不确定的,为什么?我认为这可能是一个字符串,而不是整数,所以我尝试在select.value之前放置parseint
,但它不起作用。
I got a input like this with a numbered value
<input type="radio" id="ja" name="upprepas" value="0">
<input type="radio" id="ja" name="bekanta" value="2">
But i also have like 10 other inputs, I want to count all of the inputed values. So I tried this:
var select = document.querySelector('input[name="upprepas"]:checked');
console.log(select.value);
var selecto = document.querySelector('input[name="bekanta"]:checked');
console.log(selecto.value);
var selector = select.value + selecto.value;
console.log(selector.value);
But selector is always undefined, why is that? I thought that it maybe was a string and not an integer so I tried putting parseInt
before select.value but it didn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您始终可以使用 querySelectorall 查找所有检查收音机,并循环循环并添加它们。
You can always use querySelectorAll to find all of the checked radios, and loop through their values and add them up.
您需要做的是替换
var selector = select.value + selecto.value;
atvar selector = parseint(select.value) + parseint(selecto.value); 还使用
const
或让
而不是var
,这是最佳练习^^What you would need to do is replace
var selector = select.value + selecto.value;
withvar selector = parseInt(select.value) + parseInt(selecto.value);
Also useconst
orlet
instead ofvar
, it's a best practice ^^