views:

88

answers:

5

I am attempting to use jQuery's .delegate() function to catch a submit button press and prevent the page from being reloaded.

It works fine in every browser I have tried, but Internet Explorer!

Let me explain in code, I have this HTML which is dynamically generated by JS:

<form id='submit-form'>
  <input type='text' id='blah' />
  <input type='submit' id='blahbutton' />
</form>

My Javascript looks like this:

$("body").delegate("#submit-form", "submit", function(event)
{
  // to logic here
  return false;
});

When typing in the text box and using the 'Enter' keyboard button, it performs the JS fine and the page stays as-is. But when the Submit button is clicked with the mouse, it reloads the page and submits the form.

How can I make it so clicking the button does the same as pressing enter on the input on Internet Explorer?

A: 

Try this,

<form id='submit-form' onSubmit='return false;'>
nullptr
Yes, that will prevent the form submission. There is the minor issue of the `// to logic here` bit...
T.J. Crowder
Prevents the JS from executing as well...
Valorin
If you're using jQuery, you really should be separating your behavior from your HTML structure. Instead of using an onSubmit in your form, add a listener binding inside your document ready function.
Kamikaze Mercenary
+1  A: 

Long shot here, but do you have anything else on that page with either an id or name attribute of submit-form or blahbutton? Or a global variable (e.g., window property)? Because IE has namespace issues (it conflates things it shouldn't), and more than once I've seen this sort of oddball issue resolved by discovering that there's something somewhere with a name that has the same value.

An easy way to check is to temporarily give both the form and the button completely new IDs (like flibbergibbet23 and ohmygodunicorns451) and see if the problem goes away. If so, then you know it's a weird IE name conflict and you can go on a search-and-destroy mission...

T.J. Crowder
Good suggestion :) but the id's are all very unique. (I've used friendly ones for the example)
Valorin
@Valorin: Just to be doubly-sure that you understood me, though, unfortunately IE will confuse `id` and `name` values (it mixes them together) *and* properties on the `window` object. So it's not just `id`s...
T.J. Crowder
I'm setting the name and id attr's of the element at the same time. The problem is that IE triggers the form action BEFORE it triggers the JS action, so the JS never gets run. (Cos I am using .delegate())
Valorin
@Valorin: Now that's interesting...and quite possibly an issue worth raising with the jQuery folks, if you're using the latest (1.4.2). (If you aren't, 1.4.2 had a number of improvements to `live` and `delegate`, so this may be fixed in it.)
T.J. Crowder
A: 

don't event.preventDefault() and event.stopPropagation() work for you?

Alternatively the following should work as well:

    function myFunction()
    {
        if(...)
        {
           return true;
        }
        return false;
    }

For

<form id="formId" action="#" onsubmit="return(myFunction())">
...
   <input type="submit" value="submit" />
</form>
negative
No, they don't work. Thanks for the suggestion though :)
Valorin
Btw, I just tried your code and it seems to work fine in IE - http://jsbin.com/esezu3
negative
It doesn't work for my IE installation, so maybe there is something else going on.
Valorin
A: 

One thing you might consider is using button instead of submit as your type attribute.

<button type="submit"></button>

Is the same as

<input type="submit"></button>

But if you use

<button type="button"></button> 

or

<input type="button"></button>

Then use something like:

$(function() {
    $("#buttonId").bind("click", function() {

        // ...do stuff

        //if all conditions are met

        $("formId").trigger("submit");
    });
});

Then it won't submit a form as its default action and you can attach a click listener, etc to it as needed, and have that listener function submit the form instead as long as whatever conditions you specify (if any) are met.

That way you don't have to use what is in my opinion, a bit of a hack to cancel (using e.preventDefault(), returning false, whatever...) a form submit action that didn't have to be fired in the first place. Cancelling the default behavior can sometimes lead to weird and hard to track down side-effects...so why not avoid having to do it in the first place (if you are able to, of course)?

Kamikaze Mercenary
Buttons don't trigger on the 'enter' button press in an input form.
Valorin
A: 

I figured it out.

My solution was to create actions for both the form submit, and the submit button click.

So:

  • Pressing 'Enter' in the input box triggered the Form Submit action
  • Clicking on the 'Submit' button triggered the Button Click Action

This does mean code duplication, but it now works in IE and others.

Valorin