I have written a jquery script to find the total number of checkboxes on the page and the total number that are checked. This script applies to a page with dynamically generated checkboxes, depending on the attributes of the object rendered into the page (using Django). The function then should find the ratio of checked to total checkboxes and multiply that by 5 and save that as a variable 'score'. It should then set the value of a form input field (#score) to the score vairable.
The script:
$(":checkbox").change( function(){
var checkboxes = $("input:checkbox"); //find all checkboxes
var number_of_checkboxes = checkboxes.length; //number of checkboxes
var checked_checkboxes = $("input:checkbox:checked"); //find all checked checkboxes
var number_of_checked_checkboxes = checked_checkboxes.length; //number of checked checkboxes
var score = (number_of_checked_checkboxes / number_of_checkboxes) * 5;
$("#score").val(score);
});
The issue is that, instead of setting the input field value to the score, it seems that the value is being set to the number of checked checkboxes. I'm very confused as to how this could have happened.
Aucun commentaire:
Enregistrer un commentaire