Results 1 to 2 of 2

Thread: javascript form reset function not working in mvc/jsp?

  1. #1

    Default javascript form reset function not working in mvc/jsp?

    I am porting an application from struts2 to spring 3 mvc.

    So far the forms are fine but now I have hit a problem with the javascript.

    I have a button that launches a js script function to reset the form. I can see the function is being executed via use of an alert but the form just does not reset back to the original state when the script fires.

    I have a similar script function which resets a hidden variable but when changing the value from the original '0', to '1', it actually changes the value to '1,0'.

    Also there is a script function which removes the onbeforeunload action but this also does not work.

    The same form with the same js under struts2 works fine, with all the script functions doing exactly as they are supposed to do.

    Is there anything special I have to do to get javascript to work properly in jsp under spring/mvc?

    Here are the javascript functions

    Code:
    <script language="javascript" type="text/javascript">
            function submitHandler(buttonPressed)
            {
    
                if (
                        buttonPressed == document.getElementById('acceptFullAllocation').id
                        ||
                        buttonPressed == document.getElementById('unassignFullAllocation').id
                        ||
                        buttonPressed == document.getElementById('saveAllocations').id
                   )
                    // remove window.beforeunload so these operations can continue without warnings
               {
                    window.onbeforeunload = null;
               }
    
            }
    
            function changeHandler(modifiedObject)
            {
                // mark the form as modified
                document.getElementById('formChanged').value = '1';
                
                // determine which form element was modified
                var tagObject = 'status_' + modifiedObject;
                // let the form action target know which form element has been modified
                document.getElementById(tagObject).value = '1';
                
                // add onbeforeunload event handler to advise user pf pending changes
                window.onbeforeunload = function()
                {
                    return('${sessionObject.getPropContent("Pager.changes.confirm")}');
                }
    
            }
            
            function resetForm()
            {
    
               if (confirm('${sessionObject.getPropContent("Pager.changes.reset")}'))
                    // reset the form and all navigation locks
               {
                    // mark the form as unmodified
                    document.getElementById('formChanged').value = '1';
                    
                    // remove the onbeforeunload handler
                    window.onbeforeunload = null;
    
                    // reset the form
                    document.getElementById('seatsForm').reset();
    
                    // iterate the form elements
                    $(':input', document.getElementById('seatsForm')).each(function()
                    {
                        
                        if (this.name.substring(0, 7) == 'status_')
                            // this is a modifier flag, so reset it
                        {
                            this.value = '0';
                        }
                                
                    });
    
                }
                    
                // remove window.beforeunload
                // return false here to cancel the action
                return(false);
            }
    </script>

  2. #2

    Default

    Okay after more debugging, the problem is solved and everything works as it did under struts 2.

    The submitHandler was not fired due to an onClick error when converting the code form a struts2 button to a html button and the reset function was not working due to a typo in the html link.

    The strange thing was that the value of the formChanged variable is correct when it gets to the controller because there are 2 hidden fields with the same name in the form, which obviously is a bug but struts 2 returned this double value as a single value, presumably overwriting, rather than combining in to an array. So it was a simple case of removing the duplicate formChanged variable to get that to work as expected by the controller.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •