所有,我有以下代码来检查我的所有单选按钮组是否都被选中:
jQuery("input[type=radio]").click(function(event){
var num_questions = jQuery("#num_questions").val(),
new_questions = parseInt(num_questions, 10) + 1,
radios = jQuery("input[type=radio]"),
all_answered = true;
for(var i=1; i<new_questions; i++){
alert(i);
var group = radios.filter('[name=rating_value_'+i+']');
alert(group.filter(":checked").length);
if(group.filter(":checked").length==0){
all_answered = false;
break;
}
}
if (all_answered) {
// a group was found that didn't have an answer selected
// so do something
jQuery("#no_answers").html('');
}
});
这很适合做检查,但我有一个问题。此代码在实际单选按钮被标记为已单击之前运行,因此它认为仍有一个未单击,即使那是最后一个。我该如何解决这个问题,让它像我想象的那样工作?