在 JavaScript 中循环广播组

发布于 2024-10-10 06:59:58 字数 101 浏览 0 评论 0原文

我正在尝试循环遍历无线电组并验证用户是否已使用 Javascript 进行了选择。单选组是动态的,因此运行时字段名称未知,单选组的数量也未知。用户对每个单选按钮组进行选择后,然后处理表单。

I am trying to loop through Radio groups and validate that the user has made a selection using Javascript. The radio groups are dynamic so the field names are unknown at runtime, and the number of radio groups will also be unknown. After the user has made a selection for each radio group, then process the form.

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

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

发布评论

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

评论(3

待天淡蓝洁白时 2024-10-17 06:59:58

您可以有一个地图来查找未检查的字段名称。

function add() {
    remaining[this.name] = true;
}
function remove() {
    delete remaining[this.name];
}
var form = $(this), remaining = {};
form.find(':radio').each(add).filter(':checked').each(remove);

那么 remaining 变量将是一个对象,其中保存用户尚未检查的单选按钮组的名称。

如果它是空对象,则用户已选择所有组。

有关工作示例,请查看此处:http://jsfiddle.net/thai/qtJsJ/1/

You can have a map to find the field names that are not checked.

function add() {
    remaining[this.name] = true;
}
function remove() {
    delete remaining[this.name];
}
var form = $(this), remaining = {};
form.find(':radio').each(add).filter(':checked').each(remove);

Then the remaining variable will be an object that holds the names of the radio group that the user hasn't checked.

If it is an empty object, then the user has selected all groups.

For a working example, look here: http://jsfiddle.net/thai/qtJsJ/1/

七堇年 2024-10-17 06:59:58

您可以使用“input:radio”选择器选择所有单选按钮,然后确保为每个不同的名称设置一个值。

$(document).ready(function() {
  $(this).find("input:radio").each(...)
}

You could select all radio buttons using the 'input:radio' selector and then make sure that for each distinct name a value is set.

$(document).ready(function() {
  $(this).find("input:radio").each(...)
}
牵你手 2024-10-17 06:59:58

使用纯javascript你可以尝试类似的东西

var elements = document.getElementsByTagName("input");
for(var i = 0; i<elements.length; i++)
{
    if(elements[i].type === "radio")
    {
        //dostuff
    }
}

with pure javascript you could try something like

var elements = document.getElementsByTagName("input");
for(var i = 0; i<elements.length; i++)
{
    if(elements[i].type === "radio")
    {
        //dostuff
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文