单选按钮验证(动态名称)

发布于 2024-08-22 23:28:32 字数 69 浏览 6 评论 0原文

我有一个单选按钮列表。每个单选按钮都有一个动态名称。 有没有办法检查是否全部被选中? 因为大多数无线电验证脚本使用静态名称。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2024-08-29 23:28:32

如果您知道某个容器的 ID,则可以找到带有“getElementsByTagName”的单选按钮。因此,如果您的 HTML 看起来像这样:

<form id='x-form' action='...'>
    <input type='radio' name='$[xyz}'>
    <!-- ... -->

那么您可以检查这样的单选按钮:

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 这样的框架,您的生活将会变得更加容易。

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;
}

Your life would be considerably easier if you were using a framework like jQuery.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文