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?
using jquery i would do this
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
You should be able to do:
From the jQuery documentation:
If you want to prevent just form submits:
I think that best approach to just prevent
<form>
from sending anything to anywhere is to: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/
Use event delegation to prevent all form within the body to be submitted.
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
Here is a working demo http://jsfiddle.net/pomeh/TUGAa/
Then you could use a combination of..
and
You can either disable the submit buttons
or just make the submit event do nothing
It depends what exactly are you trying to achieve.