javascript - How to cancel submit in IE8? -
i've been struggling make work quite time. have following code:
$(document).ready(function (event) { $('form').submit(function (event) { ... }); }); i've found in of places, including on jquery site, prevent form being submitted:
return false; it doesn't.
next i've read related question , answers on site , tried them out. in chrome , firefox works:
event.preventdefault(); in ie8 doesn't. i've read ie doesn't support , should use this:
event.returnvalue = false; although several people confirm worked them (e.g. event.preventdefault() function not working in ie. help?), doesn't me. i'm not sure why though. have form submit button, nothing special, but, if put piece of code, form still submitted:
$(document).ready(function (event) { $('form').submit(function (event) { event.returnvalue = false; }); }); any ideas?
one last thing: i've read should test first before calling preventdefault():
if (event.preventdefault) { event.preventdefault(); } but in ie8, event.preventdefault exists , no error thrown. how can if it's unsupported ie?
update 1: i've tried simplified version following code:
cancelsubmittest.aspx:
<%@ page language="c#" autoeventwireup="true" codebehind="cancelsubmittest.aspx.cs" inherits="htmleditor.cancelsubmittest" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script src="scripts/cancelsubmit.js" type="text/javascript"></script> </head> <body> <form id="form1" runat="server"> <div> <asp:button id="savebutton" runat="server" text="save" onclick="savebuttonclick" causesvalidation="true" validationgroup="somevalidationgroup" /> </div> </form> </body> </html> cancelsubmittest.aspx.cs
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; namespace htmleditor { public partial class cancelsubmittest : system.web.ui.page { protected void page_load(object sender, eventargs e) { } protected void savebuttonclick(object sender, eventargs e) { } } } cancelsubmit.js
$(document).ready(function (event) { $('form').submit(function (event) { event.returnvalue = false; }); }); it's pretty straight-forward example. javascript code executed form still submitted, although set event.returnvalue false. doing wrong?
update 2: i've changed javascript code in simplified project follows:
$(document).ready(function (event) { $('form').submit(function (event) { return false; }); }); it works in ie still have figure out why doesn't work in application. help.
- it work: http://jsfiddle.net/aec2u/
- the problem somewhere else in code.
- post more code/stripped down example doesn't work.
Comments
Post a Comment