17

Using the .NET Windows Forms WebBrowser control to show the preview of a page, I'm using the following approach described in this SO posting 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 as the preview also contains HTML forms, I'm looking for a similar approach to disable all form submit functionality.

I've tried:

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

But it seems that this doesn't work (read: "still loads 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 submit functionality on a HTML page, no matter whether it is a GET or a POST form?

Community
  • 1
  • 1
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

8 Answers8

37

You should be able to do:

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

From the jQuery documentation:

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

Prestaul
  • 83,552
  • 10
  • 84
  • 84
  • 1
    @pomeh, are you suggesting that it is not ok to cancel bubbling of a submit event when the question specifically asked how to cancel all submit behavior on the page? `preventDefault` is perfect in some cases, but `return false` has its place as well. – Prestaul Apr 23 '12 at 16:15
  • 1
    "cancel bubbling" !== "cancel submit". The question is how "to disable all form submit". This is done by the `event.preventDefault` method from jQuery. `return false` does this, but it also call the `event.stopPropagation` method which is not in the intent of the question. https://github.com/jquery/jquery/blob/1.7.2/src/event.js#L467 – pomeh Apr 23 '12 at 20:11
  • 1
    Ok, fair enough. I reread the question and he is not really asking what I initially thought. `return false` WILL prevent any delegated submit functionality from executing, but I still maintain that there is a place for `return false` in your toolbox. My complaint: A url does not a comment make. If you think that I have not answered his question properly then explain why. Your url "comment" just makes you look like you are espousing the religion of "stop using return false" and not bothering to think. How about a comment AND a link next time? – Prestaul Apr 23 '12 at 21:21
  • 1
    You're right, my apologizes. About the `return false`, what I personally don't like is: it's intent is "hidden", which means if you don't know well jQuery you don't understand what this does, I always prefer "explicit code" over "implicit" one but it's a matter of preference I think; and secondly in *most* cases you only want to prevent the default behavior or stop the bubbling, but not both at the same time – pomeh Apr 23 '12 at 21:28
  • 1
    @pomeh, I completely agree. It is an excellent point to make and I'm honestly glad that you were here to make it. – Prestaul Apr 23 '12 at 22:18
  • Since I've tried all answers to my post and all did _not_ work (the one in my question still works best), I'll simply mark yours as the answer. – Uwe Keim Apr 24 '12 at 03:47
  • 2
    @UweKeim *what* did not work ? I've updated my answer with a test case and additional information, have a look – pomeh Apr 24 '12 at 07:40
  • 1
    @UweKeim, there are a lot of answers on this page that "work" (i.e. they do what you asked in your question). If you are still having trouble then maybe some more details (or a link to your page) are in order. – Prestaul Apr 24 '12 at 16:01
  • Thanks, guys. I experienced that with all your solutions, my page still reloads. I'll have to further investigate, what happens. Since it is a `WebBrowser` control, I cannot simply fire up Web Developer tools to see what happens. – Uwe Keim Apr 24 '12 at 16:37
  • 1
    Why not do `$(document).on('submit', 'form', false)` to disable all future forms from submitting as well? – chowey May 15 '14 at 07:35
14

Use this:

<form id="myform" name="myform" onSubmit="search();return false;" action="">
Milad Ghiravani
  • 1,625
  • 23
  • 43
  • While this does not exactly answer the question (disabling all form submits) in my opinion is superior to other solutions, which impact all forms on a page. Also, it **disables form submission immediately**, which might be desired. Waiting for JS to load can provide a window of opportunity for a hasty user to click or submit the form in-between. It is also explicit. – SteveB Jul 11 '18 at 11:16
3

using jquery i would do this

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

with jquery you normally dont use .attr(...) to bind event listeners instead you should use the helper methods, or .bind(...)

here its the complete reference: jQuery events

Jarry
  • 1,891
  • 3
  • 15
  • 27
3

Then you could use a combination of..

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

and

$('form').unbind('submit');
Robin Maben
  • 22,194
  • 16
  • 64
  • 99
  • 3
    this does not prevent the form to being submitted, it disable the submit button, which have not the same effect – pomeh Apr 23 '12 at 15:54
  • 1
    Are you trying to `submit()` programmatically? – Robin Maben Apr 24 '12 at 05:38
  • Yes. Here is the code http://jsfiddle.net/pomeh/YW6VX/ and here is the demo http://jsfiddle.net/pomeh/YW6VX/show/ (the demo embed in jsfiddle does not work because of iframe security settings) – pomeh Apr 24 '12 at 07:57
  • `$('form').unbind('submit');` followed by `$('form').submit(false);` helped me out, thanks. – Jim Dec 05 '15 at 22:36
3

You can either disable the submit buttons

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

or just make the submit event do nothing

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

It depends what exactly are you trying to achieve.

Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
2

If you want to prevent just form submits:

I think that best approach to just prevent <form> from sending anything to anywhere is to:

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

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

JSFiddle demonstration enable/disable form submit:

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

  • the second solution disable *every* kind of interaction with the web page, which means click & form submit but also disable CSS `:hover` rules to apply for example – pomeh Apr 23 '12 at 20:13
  • & how would you simulate the `:hover` CSS selector ? – pomeh Apr 23 '12 at 20:45
  • I'm not asking *how to do* it, I'm just asking how your second solution handle it, and the answer is "it doesn't". I think that's why the answer has been downvoted, 90% of the answer doesn't answer to the question – pomeh Apr 23 '12 at 20:51
  • OK, so i need to edit it :) Thanks, I think that i should not implement :hover simulation here just now even if it is possible... – Sampo Sarrala - codidact.org Apr 23 '12 at 20:57
  • With that second solution, you would have to implement a lot of other things... `:hover` (which also means have the correct mouse cursor), click on form elements and so on... – pomeh Apr 23 '12 at 21:03
1

Use event delegation to prevent all form within the body to be submitted.

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

By the way, your Javascript code to disable all links on the page is not a good way to do. You could use instead 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/

pomeh
  • 4,742
  • 4
  • 23
  • 44
0
<style>[busy]{pointer-events:none;}</style>
<form>....</form>
<form>....</form>
<form>....</form>
<script>
function disableIt(node){
    node.setAttribute('busy','');
    // later re-enable with node.removeAttribute('busy')
}
Array.from(document.querySelectorAll('form')).forEach(disableIt);
// NOTE other functions can return on form.hasAttribute('busy') if needed
</script>

my other answer has a more specific one-off example: https://stackoverflow.com/a/64831209/965666

jimmont
  • 2,304
  • 1
  • 27
  • 29