jQuery,检查是否选中了所有单选按钮组
我有几个radiobuttongroups,我需要在检查时运行脚本。
我使用以下脚本检查是否检查了其中一个,如果没有,则将其着色。
如何制作代码,以便在检查所有radiobuttongroup时,然后运行脚本。
检查radiobuttongroup是否被检查的代码:
$('.aQuestion').each(function(){ if($(this).find('input[type="radio"]:checked').length > 0) { alert("checked"); } else { alert("not checked"); } });
radiobuttongroups(大约90个):
1. Question answer 1 answer 2 answer 3 answer 4 answer 5 2. Question answer 1 answer 2 answer 3 answer 4 answer 5
提前致谢
假设每个问题只有一个单选按钮组,您不需要迭代这些问题以便找出它们全部被选中:
var $questions = $(".aQuestion"); if($questions.find("input:radio:checked").length === $questions.length) { // All Checked }
jsFiddle演示了上述内容。
试试这个:
$(document).on('ready change','.aQuestion',function(){ if($(this).find('input[type="radio"]:checked').length > 0) { alert("checked"); } else { alert("not checked"); } });
最终代码:
jQuery('#submit').click(function(event) { event.preventDefault(); $('.aQuestion').each(function() { if($(this).find('input[type="radio"]:checked').length > 0) { $(this).addClass( "madeChoice" ); // Run css that hides the group } else { $(this).addClass( "didntMakeChoice" ); // Run css that highlight the group. } }); var $questions = $(".aQuestion"); if($questions.find("input:radio:checked").length === $questions.length) { alert("all checked"); // Send result to DB. } else { alert("Not Checked"); // Do nothing. } });