Here is my test.zip file attached. Please unzip to find test.html . run this in IE browser.
In test.html page , I select a checkbox in page1 and then go to next page . I select another checkbox in page2 and then go to next page. I select another checkbox there in page3.
Now click submit button.
I get an alert "1 item selected".
This is wrong. It should be "3 item selected" because I checked 3 checkboxes.
Could you please take a look whats wrong in my javascript function ? how do I fix it ? test.zip
JavaScriptJSPHTML
Last Comment
cofactor
8/22/2022 - Mon
Tom Beck
All of your checkboxes have name="student" making them mutually exclusive. When you check the next one, the last one becomes unchecked.
cofactor
ASKER
>>>All of your checkboxes have name="student" making them mutually exclusive.
OK. thanks but I dont want to keep separate names for checkboxes. I have kept separate ID's for them though.
Can we modify the javascript function now so that it can give the correct results ?
Basically , at the end I want to send the selected checkbox ID's to the server.
Tom Beck
Are you trying to make the checkboxes on each page mutually exclusive? If so, you can make the name for the inputs on each page unique. Like: page one name="student1", page two name="student2". Then in jQuery:
var fields = $("input[name*='Student']").serializeArray();
The * will find any name attribute that contains the word "Student" and add them to the array.
I did consider that situation. The "else" section of the adjustCount function loops through the array and removes any checkbox id where the checkbox is not checked. Array.pop will not work because it only removes the last item in the array. I cannot be certain that the checkbox just unchecked is the last item in the array.