I just spent way too long wondering why my silly page kept refreshing itself, one of the forms on the page was submitting itself over and over ... I had been refactoring some Javascipt to allow me to use it generically across pages.
My orginal code to bind an ajax call to the submit of the form was as follows:
$("#SearchForm").submit(function() { ... previous ajax code inline here ... ; return false; });
And all was fine ... except I wanted to reuse the inline code, so I put it into a function:
$("#SearchForm").submit(GetTableContent(controller, displaySelect));
And suddenly the page started refreshing automatically, making debugging it rather hard to even find the mistake. After 20 mins scanning through the jQuery documentation on form submit I noticed that "submit()" reads "Trigger the submit event of each matched element" but that "submit(fn)" reads "Bind a function to the submit event of each matched element" ... clearly the first version was being used by jQuery, where as my code should have been the second one - after all I had a function in there now?
Then I twigged it, the jQuery (fn) wants a plain basic function with no parameters ... so a quick change wrap my new function with a parameterless function resolved the problem:
$("#SearchForm").submit(function() { GetTableContent(controller, displaySelect); return false; });
Annoying ... I hate wasting 20 mins on silly things like this ...
Posted
10-09-2008 11:43 AM
by
Jak Charlton