使用jquery查找没有答案的单选按钮组

发布于 2024-10-14 04:12:47 字数 1425 浏览 0 评论 0原文

我一直在绞尽脑汁寻找更好的解决方案来解决以下情况。

想象一个页面上有很多单选按钮,然后进行一些自定义验证,突出显示尚未回答的单选按钮组。

示例 html(预先选择了一些答案):

<input type='radio' name='a' value='1' />
<input type='radio' name='a' value='2' />
<input type='radio' name='a' value='3' />
<input type='radio' name='b' value='1' checked="checked"//>
<input type='radio' name='b' value='2' />
<input type='radio' name='b' value='3' />
<input type='radio' name='c' value='1' />
<input type='radio' name='c' value='2' checked="checked"/>
<input type='radio' name='c' value='3' />
<input type='radio' name='d' value='1' />
<input type='radio' name='d' value='2' />
<input type='radio' name='d' value='3' />

我已经编写了一些 jQuery 来实现我想要的功能,但我只知道它可以做得更好/更快/更漂亮/更有效。

//select all radiobuttons
var $radios = $("input[type='radio']")
//loop through all the radios that are checked
$($radios+":checked").each(function(){
    //filter out all radios with the current name, and add something to recognize them with
    $($radios).filter("[name='" + $(this).attr("name") + "']").addClass("checked")
});

//find all radios without the class checked, and highlight them
var $unchecked_radios = $($radios + ":not(.checked)").addClass("highlight");

但我一直不知道如何将这两者结合起来,因此从整个收音机中减去有答案的那些。

(尽管我怀疑可能有更好的方法来选择具有相同名称的单选按钮组)

任何帮助将不胜感激!

问候, 卡斯帕

I've been racking my brain to find a better solution to the following situation.

Imagine a page with a LOT of radiobuttons on it, and then having some custom validation highlighting the groups of radiobuttons that haven't been answered.

example html (with some answers preselected):

<input type='radio' name='a' value='1' />
<input type='radio' name='a' value='2' />
<input type='radio' name='a' value='3' />
<input type='radio' name='b' value='1' checked="checked"//>
<input type='radio' name='b' value='2' />
<input type='radio' name='b' value='3' />
<input type='radio' name='c' value='1' />
<input type='radio' name='c' value='2' checked="checked"/>
<input type='radio' name='c' value='3' />
<input type='radio' name='d' value='1' />
<input type='radio' name='d' value='2' />
<input type='radio' name='d' value='3' />

I've already written a bit of jQuery that does what i want, but I just know it can be done better/faster/prettier/more efficiently.

//select all radiobuttons
var $radios = $("input[type='radio']")
//loop through all the radios that are checked
$($radios+":checked").each(function(){
    //filter out all radios with the current name, and add something to recognize them with
    $($radios).filter("[name='" + $(this).attr("name") + "']").addClass("checked")
});

//find all radios without the class checked, and highlight them
var $unchecked_radios = $($radios + ":not(.checked)").addClass("highlight");

But i'm stuck on how to combine those two, so subtracting the ones that have an answer from the entire population of radios.

(although i suspect there might be a better way to select groups of radiobuttons with the same name)

Any help would be greatly appreciated!

Regards,
Casper

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

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

发布评论

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

评论(3

故事与诗 2024-10-21 04:12:47

我不知道是否可以提供帮助,这里有一些代码:

$(document).ready(function() {
        $('input:not(:checked)').each(function() {
            if($('input[name='+$(this).attr('name')+']:checked').size() == 0)
            {
                $(this).addClass('highlight');
            }
        });
    });

有了这个,您只需突出显示尚未检查任何无线电的组中未经检查的无线电。
它符合您的需求吗?

I don't know if can help, here is some code :

$(document).ready(function() {
        $('input:not(:checked)').each(function() {
            if($('input[name='+$(this).attr('name')+']:checked').size() == 0)
            {
                $(this).addClass('highlight');
            }
        });
    });

With this, you only highlight the unchecked radio of groups that don't have any radio checked yet.
Does it fit your need?

紅太極 2024-10-21 04:12:47

这个怎么样?

function is_answered(valueName)
{
  var result=$('input[name='+valueName+']:checked').size();
  return result > 0;
}

How about this?

function is_answered(valueName)
{
  var result=$('input[name='+valueName+']:checked').size();
  return result > 0;
}
柠北森屋 2024-10-21 04:12:47

试试这个:

var $radios = $("input[type='radio']")
//loop through all the radios that are checked
$radios.filter(":checked")
.each(function(){
    //filter out all radios with the current name, and add something to recognize them with
    $($radios).filter("[name='" + $(this).attr("name") + "']").addClass("checked")
})
.end()
.filter(":not(.checked)").addClass("highlight");

根据用例,这可能会更好:

$("input[type='radio']").filter(":not(:checked)").addClass("highlight");

但我不知道您是否还需要“checked”类来做其他事情。

Try this:

var $radios = $("input[type='radio']")
//loop through all the radios that are checked
$radios.filter(":checked")
.each(function(){
    //filter out all radios with the current name, and add something to recognize them with
    $($radios).filter("[name='" + $(this).attr("name") + "']").addClass("checked")
})
.end()
.filter(":not(.checked)").addClass("highlight");

Depending on the usecase, this might be nicer:

$("input[type='radio']").filter(":not(:checked)").addClass("highlight");

But I do not know if you need the "checked" class for something else too.

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