我们如何使用DOM访问单选按钮的值? [英] How can we access the value of a radio button using the DOM?
本文介绍了我们如何使用DOM访问单选按钮的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用DOM访问单选按钮的值?
我们有单选按钮:
< input name =sextype =radiovalue =male> ;
< input name =sextype =radiovalue =female>
他们在一个名称为 form1
。当我尝试
document.getElementByName(sex)。value
pre>
它总是返回男,而不考虑所选值。
解决方案只是为了生成Canavar的非常有用的功能:
function getRadioValue(theRadioGroup)
{
var elements = document.getElementsByName(theRadioGroup);
for(var i = 0,l = elements.length; i< l; i ++)
{
if(elements [i] .checked)
{
返回元素[i] .value;
}
}
}
...现在请参考:
getRadioValue('sex');奇怪的是,这样的东西并不是prototype.js的一部分。 $ $ $ $ $ $ $ $ $ $ $ $ $
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">
They are inside a form with name
form1
. When I trydocument.getElementByName("sex").value
it returns 'male' always irrespective of the checked value.
解决方案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');
Strange that something like this isn't already a part of prototype.js.
这篇关于我们如何使用DOM访问单选按钮的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文