单选按钮验证(动态名称) [英] Radio button validation (dynamic name)
本文介绍了单选按钮验证(动态名称)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个单选按钮列表.每个单选按钮都有一个动态名称.有没有办法检查它们是否都被选中?因为大多数无线电验证脚本都使用静态名称.
I have a list of radio buttons. Each radio button has a dynamic name. Is there a way to check if they are all selected? Because most radio validation scripts uses a static name.
推荐答案
如果您知道某个容器的 id,您可以找到带有getElementsByTagName"的单选按钮.因此,如果您的 HTML 如下所示:
If you know the id of some container, you can find the radio buttons with "getElementsByTagName". Thus if your HTML looks something like this:
<form id='x-form' action='...'>
<input type='radio' name='$[xyz}'>
<!-- ... -->
然后你可以像这样检查单选按钮:
then you could check the radio buttons like this:
function allRadioButtonsSelected(formId) {
var form = document.getElementById(formid);
var inputs = form.getElementsByTagName('INPUT');
for (var i = 0; i < inputs.length; ++i) {
if (inputs[i].type.toLowerCase == 'radio' && !inputs[i].checked)
return false;
}
return true;
}
如果您使用像 jQuery 这样的框架,您的生活会轻松很多.
Your life would be considerably easier if you were using a framework like jQuery.
这篇关于单选按钮验证(动态名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文