Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable submit functionality for all forms on a HTML page

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?

like image 568
Uwe Keim Avatar asked Apr 23 '12 15:04

Uwe Keim


People also ask

How do I stop HTML form from submitting?

We use the preventDefault() method with this event to prevent the default action of the form, that is prevent the form from submitting. Example: HTML.

How do you disable form submit button until all input fields are filled?

Just click f12 in your browser, find the submit button in the html, and then remove the disabled ! It will submit the form even if the inputs are empty.

How do I disable form action in html?

The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the <input> element usable. Tip: Disabled <input> elements in a form will not be submitted!


2 Answers

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 ).

like image 116
Prestaul Avatar answered Sep 17 '22 14:09

Prestaul


Use this:

<form id="myform" name="myform" onSubmit="search();return false;" action="">
like image 32
mghhgm Avatar answered Sep 18 '22 14:09

mghhgm