How can we access the value of a radio button using the DOM?(我们如何使用 DOM 访问单选按钮的值?)
问题描述
我们如何使用 DOM 访问单选按钮的值?
How can we access the value of a radio button using the DOM?
例如.我们有单选按钮:
For eg. we have the radio button as :
<input name="sex" type="radio" value="male">
<input name="sex" type="radio" value="female">
它们位于名为 form1 的表单中.当我尝试
They are inside a form with name form1. When I try
document.getElementByName("sex").value
无论检查值如何,它总是返回男性".
it returns 'male' always irrespective of the checked value.
推荐答案
只是为了生成" Canavar 非常有用的功能:
Just to "generify" Canavar's very helpful function:
function getRadioValue(theRadioGroup)
{
var elements = document.getElementsByName(theRadioGroup);
for (var i = 0, l = elements.length; i < l; i++)
{
if (elements[i].checked)
{
return elements[i].value;
}
}
}
... 现在将这样引用:
... which would now be referenced thusly:
getRadioValue('sex');
奇怪的是,这样的东西还不是prototype.js的一部分.
Strange that something like this isn't already a part of prototype.js.
这篇关于我们如何使用 DOM 访问单选按钮的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我们如何使用 DOM 访问单选按钮的值?
基础教程推荐
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
