views:

273

answers:

2

Strange problem going on here. I'm developing a JSR168 portlet that is using Spring and the Spring form taglib.

I have a search form on a page to go through a catalog of reports based on the criteria the user selects, and for the most part it works fine. However, there is one sequence of events that is triggering an error.

I have a JavaScript function that I found to clear the 7 criteria the user can specify. It clears all of the textboxes, sets the selected index of all drop down lists to 0, and defaults a group of radio buttons to the one I want defaulted. So that's all well and good and it works just fine.

Following are 2 scenarios, 1 that demonstrates it working as intended, and 1 showing the bug.

  • Type in something
  • Click Search
    • Results are shown
  • Click Search
    • Results are shown (same as before)
  • Type in something new and click search
    • Results are shown (new, correct results)

Here's the error

  • Type in something
  • Click Search
    • Results are shown
  • Click "Clear Form" which runs the JavaScript
    • Form is cleared
  • [Optional, it doesn't change anything: type in something]
  • Click Search
    • No change in results <-- Here's the bug
  • Click Search again
    • Proper results are now shown

Here's the JavaScript for reference (I have no idea if this is good or bad JS, I haven't written any in the past)

function clearForm(oForm) {

  var elements = oForm.elements;

  oForm.reset();

  for(i=0; i<elements.length; i++) {

      field_type = elements[i].type.toLowerCase();

      switch(field_type) {

        case "text":
        case "password":
        case "textarea":
              case "hidden":

          elements[i].value = "";
          break;

        case "radio":
        case "checkbox":
            if (elements[i].checked) {
              elements[i].checked = false;
          }
          break;

        case "select-one":
        case "select-multi":
                    elements[i].selectedIndex = 0;
          break;

        default:
          break;
      }
   }
}
A: 

Couldn't you just use:

<input type="reset" />

to clear your form? I'm fairly sure that it sets the form as it was when the page loads.

Juddling
Unfortunately it's not that simple. I load the form with data from the backing bean (since there are 7 fields, this is nice for the user as it lets them see what they just searched for and only edit 1 thing instead of having to retype it all). Because this is the default data loaded, doing reset simply returns to that data as opposed to actually blanking out text fields and defaulting the DDLs to the first item in them.
and you would like the inputs to return to their original values?would it be possible for you to store them as variables in javascript?
Juddling
+1  A: 

I got around this problem by changing the clear form Javascript to only clear the boxes I specify. I'm assuming that the generalized function posted up above was clearing some hidden value placed by Spring and causing the bug.

If Spring maintains a viewstate by a hidden field (like JSF can do), then you're entirely correct. Easy/obvious fix would be to skip hidden fields in reset function. I.e. remove `case "hidden":` from your switch.
BalusC