简体   繁体   English

TwitterBootStrap提交按钮仍然提交表单

[英]TwitterBootStrap Submit Button still submits the form

I have the following Bootstrap submit button with an onclick function. 我有以下Bootstrap提交按钮和onclick功能。

@Html.Bootstrap().SubmitButton().Text("Submit Final").Id("btnSubmitBottom").Style(ButtonStyle.Primary).HtmlAttributes(new { onclick = "return SubmitCheck('Submit Final')", name = "command", value = "Submit Final" }).Disabled(blnButtonsDisabled)

SubmitCheck has this ajax code that calls another function and returns a text value. SubmitCheck有这个调用另一个函数的ajax代码并返回一个文本值。 Based off of that value I display a confirm box (true) to the user or a Bootbox modal(false). 基于该值,我向用户显示一个确认框(true)或一个Bootbox模式(false)。

               $.ajax({
                    type: "GET",
                    url: "professionalismSubmitFinal",
                    data: { teacherID: CurrentTeacherID },
                    dataType: "text",
                    error: function (xhr, status, error) {
                    },
                    success: function (textVal) {
                        if (textVal == 'True') {
                            if (confirm("Are you sure you want to submit this as final?")) {
                                $('#IsPublic').val(true);
                                $('#IsFinal').val(true);
                                submitFormOkay = true;
                                return true;
                            }
                            else {
                                $('#IsFinal').val(false);
                                return false;
                            } //cancelled from modal
                        } else {
                            bootbox.alert("You cannot Submit until this has been Submitted as Final.");
                            return false;
                        }
                    }
                });

The problem is when the Ajax call returns 'False' the bootbox alert displays correctly but then the form also submits. 问题是当Ajax调用返回'False'时,bootbox警报会正确显示,但表单也会提交。

What am I doing incorrectly? 我做错了什么? I thought the onclick = return SubmitCheck would prevent this behavior? 我以为onclick = return SubmitCheck会阻止这种行为吗?

Maybe event.preventDefault(); 也许event.preventDefault(); on the button click event will help you 按钮点击事件将帮助您

$("#yourButton").click(function(event) {
    event.preventDefault();
});

Hope I understood your problem correctly. 希望我能正确理解你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM