使用jquery查找没有答案的单选按钮组
我一直在绞尽脑汁寻找更好的解决方案来解决以下情况。
想象一个页面上有很多单选按钮,然后进行一些自定义验证,突出显示尚未回答的单选按钮组。
示例 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道是否可以提供帮助,这里有一些代码:
有了这个,您只需突出显示尚未检查任何无线电的组中未经检查的无线电。
它符合您的需求吗?
I don't know if can help, here is some code :
With this, you only highlight the unchecked radio of groups that don't have any radio checked yet.
Does it fit your need?
这个怎么样?
How about this?
试试这个:
根据用例,这可能会更好:
但我不知道您是否还需要“checked”类来做其他事情。
Try this:
Depending on the usecase, this might be nicer:
But I do not know if you need the "checked" class for something else too.