如何使用js获取选中的单选按钮值
我正在使用此代码来获取当前选定的单选按钮的值,但它不起作用。
var mailcopy = document.getElementById('mailCopy').value;
如何使用 Javascript 获取当前选定的单选按钮值?
I am using this code to get the value of currently selected radio button, but it doesn't work.
var mailcopy = document.getElementById('mailCopy').value;
How to get the currently selected radio button value using Javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
HTML
JS
HTML
JS
如果您使用 jQuery,以下代码将适合您。
If you are using jQuery, following code will work for you.
单选按钮分为具有相同名称和不同 id 的组,其中一个按钮的 check 属性设置为 true,因此循环遍历它们直到找到它。
Radio buttons come in groups which have the same name and different ids, one of them will have the checked property set to true, so loop over them until you find it.
检查这个
示例
check this
Example
也许我在这里遗漏了一些东西,但是好的旧标准 JS 不能工作吗?我的意思是:
......这当然只有在为您的无线电输入元素提供“值”时才有效。
在上面的示例中,该值应为“红色”或“蓝色”。
Maybe I'm missing something here, but wouldn't the good old standard JS work? I mean:
... which is only valid of course if have provided "value" for your radio input elements.
The value should be either 'red' or 'blue' in the above example.
一种更简单的方法是使用全局 js 变量,该变量仅保存单击的单选按钮的 id。这样您就不必浪费代码在无线电列表中寻找选定的值。
当我有一个动态生成的包含 100 个或更多单选按钮的列表时,我就这样做了。通过它们旋转(几乎察觉不到)很慢,但这是一个更简单的解决方案。
您也可以使用 id 来执行此操作,但您通常只需要值。
A simpler way of doing this is to use a global js variable that simply holds the id of the clicked radio button. Then you don't have to waste code spinning thru your radio lists looking for the selected value.
I have done this in cases where I have a dynamically generated list of 100 or more radio buttons. spinning thru them is (almost imperceptible) slow, but this is an easier solution.
you can also do this with the id, but you usually just want the value.
你可以使用这个
或者,对于旧版本的jquery
you can use this
or, for older version of jquery
由于您想使用纯 JavaScript 来获取它,因此可以使用以下代码
Since you want to get it using plain javascript, you can use the following code
可能不是最有效的方法...但我在每个单选按钮上使用了 ID(这只是因为我没有将其作为实际表单传递,它只是原始字段)。
然后,我调用一个带有按钮的函数,该函数检查每个单选按钮以查看是否已选中。它使用
.checked
函数来执行此操作。如果将此设置为 true,我可以更改另一个变量的值。希望这在某种程度上有用,
Beanz
Possibly not the most efficient way... but I have used an ID on each radio button (this is just because I'm not passing it as an actual form, it is just the raw fields).
I then call a function with a button, which checks each radio button to see if it is checked. It does this using the
.checked
function. If this is set to true I can change the value of another variable.Hope this is useful, in someway,
Beanz
您可以做一些与 Beanz 的答案非常相似的事情,但不使用 ID,而是使用类来减少冗余。
You could do something very similar to Beanz's answer but instead of using IDs, use classes to reduce redundancy.
大家可以测试一下这个例子,很容易理解。
// JavaScript
all of you can test this example and easy to understand.
// javascript
试试这个,我希望这个能起作用
Try this, I hope this one will work
如果您可以使用 jQuery“Chamika Sandamal”答案是正确的方法。如果您无法使用 jQuery,您可以执行以下操作:
注释:
以下是输入单选的示例:
If you can use jQuery "Chamika Sandamal" answer is the correct way to go. In the case you can't use jQuery you can do something like this:
Notes:
Here is an example of input radios:
用途:
这将返回所选单选按钮的值。
Use:
This will return the value of the selected radio button.
请尝试这个:
Please try this:
嘿,你必须这样做。
Hy, you have to do it this way.
使用
element.checked
属性。Use the
element.checked
property.