0

在一个巨大的网络应用程序中,我需要一个提交表单的解决方法。

为了覆盖内置的 javscript 方法,我尝试了这个示例,但我什至没有收到警报弹出窗口。

我对覆盖提交功能的想法是这样的:

function submit(event) {
        var target = event ? event.target : this;
        $.ajax({
            url: target.action,
            data: $('form').serialize(), // &nd someone knows how I can get the serialize() of the form I just clicked?
            success: function (data) {
                alert('done');
            },
            error: function (data) {
                alert('error (check console log)');
                console.log(data);
            }
        });
        //this._submit();
        return false;
    }

到处使用提交的方式并不一致。原始代码中的可能性:

<button onclick="form.submit">
onclick="calltoFunction();" // where inside the function the submit is called
<button type="submit" // the normal way

我想我的想法是可能的,因为我之前覆盖了 js-functions。但是我应该如何处理提交部分。欢迎任何建议,想法,修复!

4

4 回答 4

2

将事件绑定到您的按钮,如下所示:

$('#my_button').click(
    function(event){
        event.preventDefault(); //This will prevent the buttons default submit behavior
        ...//The rest is up to you!
}
);
于 2012-10-29T15:22:41.153 回答
0
function submit(form) {
    $.ajax({
        url: $(form).attr('action'),
        data: $(form).serialize(),
        success: function (data) {
            alert('done');
        },
        error: function (data) {
            alert('error (check console log)');
            console.log(data);
        }
    });
    return false;
}

<form id="my-form" action="/my-action-file.php" method="post" onsubmit="return submit('#my-form');">
    <!-- my form content -->
</form>
于 2012-10-29T15:24:21.860 回答
0

我怀疑你会发现这很有用:

$('#yourForm').beforeSubmit(function()
{
    return doWhateverYouLikeAndReturnTrueToContinueFormSubmition($(this));
});

这将在任何其他表单提交代码之前执行 doWhateverYouLikeAndReturnTrueToContinueFormSubmition

于 2012-10-29T15:27:29.487 回答
0

你可以试试这个:

function submit(event) {
    var target = event ? event.target : this;
    $.ajax({
        url: target.action,
        type: "POST",
        data: $('form').serialize(), // &nd someone knows how I can get the serialize() of the form I just clicked?
        success: function (data) {
            alert('done');
        },
        error: function (data) {
            alert('error (check console log)');
            console.log(data);
        }
    });
    //this._submit();
    return false;
    }
于 2012-10-29T15:34:53.523 回答