Disable submit functionality for all forms on a HT

2020-03-12 03:57发布

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?

7条回答
孤傲高冷的网名
2楼-- · 2020-03-12 04:01

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

查看更多
Ridiculous、
3楼-- · 2020-03-12 04:06

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

查看更多
We Are One
4楼-- · 2020-03-12 04:07

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/

查看更多
我只想做你的唯一
5楼-- · 2020-03-12 04:10

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/

查看更多
\"骚年 ilove
6楼-- · 2020-03-12 04:12

Then you could use a combination of..

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

and

$('form').unbind('submit');
查看更多
家丑人穷心不美
7楼-- · 2020-03-12 04:23

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.

查看更多
登录 后发表回答