Disable submission of functions for all forms on the HTML page.

Using the .NET Windows Forms WebBrowser control to show a preview of the page, I use the following approach described in this post to disable all links on the page:

 $(function() { $('a').click(function() { $(this).attr('href', 'javascript:void(0);'); }); }); 

Since the HTML page I want to show, since the preview also contains HTML forms, I am looking for a similar approach to disable all form submission features.

I tried:

 $(function() { $('form').attr('onsubmit', 'return false'); }); 

But it doesn't seem to work (read: "still loading the page") for a form like:

 <form id="myform" name="myform" onsubmit="return search()" action=""> 

which I have on the page.

So my question is:

What is the best way to disable all form submission features on an HTML page, whether it is a GET or POST form?

+5
javascript jquery forms
source share
7 answers

You must be able to:

 $('form').submit(false); 

From the jQuery documentation :

In jQuery 1.4.3, you can now pass false instead of an event handler. This will bind an event handler equivalent to: function () {return false; }. This function can be removed later by calling: .unbind (eventName, false).

+20
source share

Then you could use a combination ..

 $('form :submit').attr("disabled", "disabled"); 

and

 $('form').unbind('submit'); 
+3
source share

You can disable submit buttons

 $('form :submit').attr("disabled", "disabled"); 

or just make the send event do nothing

 $('form').submit(function(e){ e.preventDefault(); }); 

It depends on what exactly you are trying to achieve.

+3
source share

using jquery i would do it

 $('form').submit(function(event){event.preventDefault();}); 

with jquery you usually don't use .attr(...) to bind event listeners, you should use helper methods or .bind(...) instead

here is its full link: jQuery events

+2
source share

If you want to prevent submission of forms only:

I think the best approach is just to prevent the <form> from posting anything anywhere:

 $('form').submit( function(e){ e.preventDefault(); }); 

This disables all forms on the page, no need to do individually for each form.

JSFiddle demo enable / disable submit form:

http://jsfiddle.net/cDLsw/6/

+2
source share

Use event delegation to prevent the submission of the entire form in the body.

 $("body").on("submit", "form", function( event ){ event.preventDefault(); }); 

By the way, your Javascript code to disable all links on a page is not a good way . Instead, you could use something like

 // use "bind" instead of "on" if you use a jQuery version prior to 1.7 $("a").on( "click", function( ev ) { ev.preventDefault(); }); // or use event delegation $("body").on( "click", "a", function( ev ) { ev.preventDefault(); }); // the power of event delegation in action: // disable BOTH links and form with one event handler $("body").on( "click", "a, form", function( ev ) { ev.preventDefault(); }); 

Here is a working demo http://jsfiddle.net/pomeh/TUGAa/

0
source share

Use this:

 <form id="myform" name="myform" onSubmit="search();return false;" action=""> 
0
source share

All Articles