All,
I’ve got the following code to run through when a radio button is clicked to see if there is at least one entry in the group(s) choosen.
jQuery("input[type=radio]").click(function(event){
alert('it is clicked');
var num_questions = jQuery("#num_questions").val();
var new_questions = parseInt(num_questions) + 1;
for(var i=1; i<new_questions; i++){
var radios = jQuery("input[type=radio]");
var group = radios.filter('[name=rating_value_'+i+']');
if(group.filter(":checked").length==0){
var answers_to_questions = "false";
return false;
}else{
var answers_to_questions = "true";
jQuery("#no_answers").html('');
}
//alert((group.filter(":checked").length)?"checked":"not checked");
}
});
I get the alert so I know it’s getting there, however, the radio button is not being checked. When I remove this code however, it works fine. Any ideas why this would cause this to happen?
Thanks in advance!
Returning
falsefrom an event handler prevents the default action for that event from occurring. In the case of a click on a radio button the default action is to select that radio button (and deselect the previously selected one in its group).I can’t understand what you’re trying to achieve here. “at least one” doesn’t make sense since only one radio at most can be checked in a given group. Less than one in a group can happen only if none were checked initially, because the user can’t un-check all radios in a given group.
Having said that, in a general sense your code can be simplified: