Andre
Andre

Reputation:

Javascript preventing Spring form from being POSTed

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.

Here's the error

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;
      }
   }
}

Upvotes: 0

Views: 705

Answers (2)

Andre
Andre

Reputation:

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.

Upvotes: 1

Juddling
Juddling

Reputation: 4690

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.

Upvotes: 0

Related Questions