【问题标题】:Jquery Submit not workJquery提交不起作用
【发布时间】:2012-04-13 00:32:04
【问题描述】:

我有一个最初为$(form).submit() 工作的函数。我不得不修改它以在$("#savebutton").click() 上工作,因为主视图中已经有一个 form.sumbit() 函数。唯一的问题是,当单击按钮并且状态有效时,表单停止提交。 旧代码

$(document).ready(function () {
    $("form").submit(function (e) {
        e.preventDefault(); //prevent default form submit
        $.ajax({
            url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
            data: { id: '@Model.ClientId' },
            success: function (data) {
                showMsg(data);
            },
            cache: false
        });
    });
});

function showMsg(hasCurrentJob) {
    if (hasCurrentJob == "True") {
        alert("The current clients has a job in progress. No changes can be saved until current job completes");
        return false;
    } else {
       $("form").unbind('submit').submit();
    }
}

当前代码

$(document).ready(function () {

    $("#saveButton").click(function (e) {
        e.preventDefault(); //prevent default form submit
        $.ajax({
            url: '@Url.Action("HasJobInProgress", "Shared")/',
            data: { id: '@Model.ClientId' },
            success: function (data) {
                showMsg(data);
            },
            cache: false
        });
    });

    function showMsg(hasCurrentJob) {
        if (hasCurrentJob == "True") {
            alert("The current clients has a job in progress. No changes can be saved until current job completes");
            return false;
        } else {
            $("form").submit();
        }
        return true;
    }
});

更新(还是不行)

$(document).ready(function () {

    $("#saveButton").click(function (e) {
        if ($(e.currentTarget).data('shouldSubmit')) return;
        e.preventDefault(); //prevent default form submit
        $.ajax({
            url: '@Url.Action("HasJobInProgress", "Shared")/',
            data: { id: '@Model.ClientId' },
            success: function (data) {
                showMsg(data, e);
            },
            cache: false
        });
    });

    function showMsg(hasCurrentJob, e) {
        if (hasCurrentJob == "True") {
            alert("The current clients has a job in progress. No changes can be saved until current job completes");
            return false;
        } else {
            $(e.currentTarget).data('shouldSubmit', true);
            $("#saveButton").click();
            $(e.currentTarget).data('shouldSubmit', null);
        }
        return true;
    }
});

【问题讨论】:

    标签: jquery .net partial-views


    【解决方案1】:

    This answer 使用了一种可能有效的技术。基本上以编程方式再次单击相同的按钮,并使用标志来确定它是否在确认方法中被单击(在您的情况下为showMsg)。

    【讨论】:

    • 我很难理解,但确实尝试过。将在上面发布。如果您看到任何内容,请告诉我。
    • @atbyrd,我看不出你的更新版本有什么明显的问题。我建议您尝试使用调试器单步执行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多