Submit adding back in checkboxes that were unchecked in javascript

G

Glenn

The following script is supposed to have high categories as radio
buttons and mid categories as check boxes. Clicking the radio button
is supposed to select all checkboxes under it and submit the form.

RADIO (ID = 0001-1 VALUE="")
CHECK (ID=0001-0011)
CHECK (ID=0001-0012)
RADIO (ID = 0001-2 VALUE="")
CHECK (ID=0001-0021)
CHECK (ID=0001-0022)

The code appears to be checking and unchecking the correct checkboxes.
But when the submit occurs, the buttons that were unchecked become
checked again.


function handleHighCatSelect(obj)
{
var highCatName = obj.name;
var highCatNameInMidCat = highCatName.substring(0,5) + "00" +
highCatName.substring(5,6);
alert(highCatName);
var length = document.frm.elements.length;
for (var i=0;i<length;i++)
{
var formObj = document.basicSearchMFSFrm.elements;
var currName = formObj.name;
var currValue = formObj.value;

if ((formObj.type == 'radio' || formObj.type == 'checkbox')
&& (currName.indexOf('0001-') != -1) )
{
if ((currName.indexOf(highCatNameInMidCat) != -1) ||
(currName == highCatName ) )
{
formObj.checked = true;
}
else
{
formObj.checked = false;
}
}
}
frm.submit();
}
 
G

Grant Wagner

Glenn said:
The code appears to be checking and unchecking the correct checkboxes.
But when the submit occurs, the buttons that were unchecked become
checked again.

I'm guessing you have neglected to provide a <FORM ACTION="...">, so the
page is submitting to itself. When it does this, the page reloads in
it's default state.

If you want to preserve the state of the <FORM>, you need to either:

1) store the state in cookies on the client before the form submits,
then when the page reloads, read that cookie and restore the state using
client-side JavaScript.

2) remember the state on the server using some server-side technology
(PHP, JSP, ASP, etc) and when you serve up the page after the GET/POST,
restore the state from the server.
 
G

Glenn

What I finally found was that my javascript was working as expected.
The problem was that the page had hidden fields to keep track of
selections on other pages. This page accidentally had the wrong hidden
fields on the page and the old selections were being resubmitted.
Thanks for your help.
 

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top