Submit adding back in checkboxes that were unchecked in javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Glenn

    #1

    Submit adding back in checkboxes that were unchecked in javascript

    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 handleHighCatSe lect(obj)
    {
    var highCatName = obj.name;
    var highCatNameInMi dCat = highCatName.sub string(0,5) + "00" +
    highCatName.sub string(5,6);
    alert(highCatNa me);
    var length = document.frm.el ements.length;
    for (var i=0;i<length;i+ +)
    {
    var formObj = document.basicS earchMFSFrm.ele ments[i];
    var currName = formObj.name;
    var currValue = formObj.value;

    if ((formObj.type == 'radio' || formObj.type == 'checkbox')
    && (currName.index Of('0001-') != -1) )
    {
    if ((currName.inde xOf(highCatName InMidCat) != -1) ||
    (currName == highCatName ) )
    {
    formObj.checked = true;
    }
    else
    {
    formObj.checked = false;
    }
    }
    }
    frm.submit();
    }

  • Grant Wagner

    #2
    Re: Submit adding back in checkboxes that were unchecked in javascript

    "Glenn" <glenn_remar@ya hoo.com> wrote in message
    news:1106070661 .141310.283840@ z14g2000cwz.goo glegroups.com.. .[color=blue]
    > The code appears to be checking and unchecking the correct checkboxes.
    > But when the submit occurs, the buttons that were unchecked become
    > checked again.[/color]

    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.

    --
    Grant Wagner <gwagner@agrico reunited.com>
    comp.lang.javas cript FAQ - http://jibbering.com/faq


    Comment

    • Glenn

      #3
      Re: Submit adding back in checkboxes that were unchecked in javascript

      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.

      Comment

      Working...