简体   繁体   English

出现错误时阻止表单提交

[英]Prevent Form from Submitting if there are Errors

Before I get into the problem details, I still need to tell you about this form I am creating. 在深入研究问题详细信息之前,我仍然需要向您介绍我正在创建的此表单。 I have a Registration form and once the user submits the form by clicking the Submit button, it will NOT go directly to a Successfully Registered page. 我有一个注册表单,一旦用户单击“提交”按钮提交表单,它就不会直接进入“成功注册”页面。 The user will be seeing a Confirmation page prior to that. 在此之前,用户将看到“确认”页面。 In this page, the user will see all the data he inputted for him to review. 在此页面中,用户将看到他输入供他查看的所有数据。 Below it are the Confirm button and the Return button (if user still likes/needs to edit his details, it will then show the form for him to edit once this button is clicked). 在其下方是“确认”按钮和“返回”按钮(如果用户仍然喜欢/需要编辑其详细信息,则单击该按钮后,它将显示供他编辑的表单)。 But here's the thing, the Registration form page and the Confirmation page are in just the same page. 但是,注册表单页面和确认页面位于同一页面中。 What I did is that when the user submits the form, it will hide some elements including the Submit button and then just show the details he inputted. 我所做的是,当用户提交表单时,它将隐藏一些元素,包括“提交”按钮,然后仅显示他输入的详细信息。 When the user clicks the Return button on the Confirmation page, it will just then show again the hidden fields so the user can edit his details. 当用户单击“确认”页面上的“返回”按钮时,它将再次显示隐藏的字段,以便用户可以编辑其详细信息。

What I did in preventing the form from submitting when there are errors is that I disabled the submit button. 我在出现错误时阻止表单提交的做法是禁用了提交按钮。 But it is not working. 但这是行不通的。 I am using bootstrap for my form so when there are errors, the input fields' borders would turn red and would obtain a class has-error . 我为表单使用了引导程序,因此当出现错误时,输入字段的边框将变为红色,并将获得类has-error Here's what I did: 这是我所做的:

$("td .form-group").each(function() {
    if($(this).hasClass('has-error') == true) {
        $('#submit').attr('disabled', false);
    } else { 
        $('#submit').attr('disabled',true);
    }
});

But again, it is not working. 但同样,它不起作用。 I also googled some jQueries like the .valid() and .validate() functions but I'm not really sure about it and also didn't work for me. 我也一派像一些jQueries .valid().validate()函数,但我真的不知道这件事,也没有为我工作。

I also did this code where the Submit button should disable when required fields are still empty. 我还执行了此代码,其中当必填字段仍然为空时,应禁用“提交”按钮。 And it is perfectly working: 它是完美的工作方式:

$('#submit').attr('disabled',true);
$('input[id^="account"]').keyup(function() {
    if(($('#profile-company_name').val().length !=0) && ($('#account-mail_address').val().length !=0) && ($('#account-confirmemail').val().length !=0) && ($('#account-login_name').val().length !=0) && (($('#account-password').val().length !=0)) && ($('#account-confirmpassword').val().length !=0)) {
        $('#submit').attr('disabled', false);
    } else {
        $('#submit').attr('disabled',true);
    }
});

I hope you understand my problem. 希望你能理解我的问题。 I will make it clearer if this confuses you. 如果这让您感到困惑,我会更清楚地说明。

What I did in preventing the form from submitting when there are errors is that I disabled the submit button. 我在出现错误时阻止表单提交的做法是禁用了提交按钮。 But it is not working. 但这是行不通的。

When is it checking for errors? 什么时候检查错误? It needs to disable the submit button at the same time it is checking for errors. 它需要在检查错误的同时禁用“提交”按钮。 Your code doesn't work because there's no event telling it WHEN to execute. 您的代码无效,因为没有事件告诉它何时执行。 WHEN do you want submit button to be disabled? 您何时要禁用“提交”按钮?

Do you want it triggered when the field is validated or when the form is submitted? 是否要在验证字段或提交表单时触发它?

You can't really tie it to the submit button unless you want to click it first to validate the form fields, and then again to submit validated fields. 除非您要先单击它以验证表单字段,然后再次单击以提交经过验证的字段,否则您实际上不能将其绑定到“提交”按钮。 Then you'll need to figure out how to tell it that it's been validated like by a class, maybe? 然后,您需要弄清楚如何告诉它已像某个班级一样经过验证了吗? Only accept inputs that hasClass('valid')? 只接受具有hasClass('valid')的输入?

below are the changes 以下是更改

$(".form-group").find("td").each(function() {
    if($(this).hasClass('has-error')) {
        $('input[type="submit"]').prop('disabled', false);
    } else { 
        $('input[type="submit"]').prop('disabled', true);
    }
});

Try the following 尝试以下

$('#submit').click(function(){
    var error = false;
    $("td .form-group").each(function() {
        if($(this).hasClass('has-error') == true) {
            error = true;
            return false; //break out of .each                
        }
    });
    return !error;
});

You can achieve this by maintaining 2 sections. 您可以通过维护2个部分来实现此目的。

1. Form section 1.表格部分

<form id="form1">
  <input type="text" id="name" />
  <input type="email" id="email" />
  <input type="button" id="confirm" value="Confirm" />
</form>

2. Confirm section 2.确认部分

<div id="disp_data" style="display: none;">
  <lable>Name: <span id="name_val"></span></lable>
  <lable>Email: <span id="email_val"></span></lable>
  <input type="button" id="return" value="Return" />
  <input type="button" id="submit" value="Submit" />
</div>

You have to submit the form by using js submit method on validating the form in confirm section (When the user clicks on submit button) 您必须通过在确认部分中的表单验证时使用js提交方法来提交表单(当用户单击“提交”按钮时)

$("#submit").click(function(){
    var error_cnt = false;
    if($("#name").val() == '') {
      error_cnt = true;
        alert("Enter Name");      
    }

    if($("#email").val() == '') {
      error_cnt = true;
        alert("Enter Email");
    }

   if(error_cnt == false) {
        $("#form1").submit();
   } else {
    $("#disp_data").hide();
    $("#form1").show();
   }

Demo 演示版

You have to prevent the form from sumition by return back a boolean false so that it will stop the execution. 您必须通过返回布尔布尔值false来阻止表单求和,以使其停止执行。

$('#submit').click(function(){    

 var ret = (($('#profile-company_name').val().length !=0) && ($('#account-mail_address').val().length !=0) && ($('#account-confirmemail').val().length !=0) && ($('#account-login_name').val().length !=0) && (($('#account-password').val().length !=0)) && ($('#account-confirmpassword').val().length !=0));
 if(!ret) return false;

});

If you want to disable the submit button in case of any error you need to monitor the changes of each input fields. 如果要在发生任何错误时禁用提交按钮,则需要监视每个输入字段的更改。 so better to give a class name to all those input fields like commonClass 因此最好为所有这些输入字段(例如commonClass提供一个类名

then 然后

function validation_check(){

 var ret = (($('#profile-company_name').val().length !=0) && ($('#account-mail_address').val().length !=0) && ($('#account-confirmemail').val().length !=0) && ($('#account-login_name').val().length !=0) && (($('#account-password').val().length !=0)) && ($('#account-confirmpassword').val().length !=0));
 return ret;

}

$("#submit").prop("disabled",true)

$(".commonClass").change(function(){
    if(validation_check()){
      $("#submit").prop("disabled",false)
    }
    else {
    $("#submit").prop("disabled",true)
    }
});

please use onsubmit attribute in the form element and write a javascript function to return false when there is any error. 请在表单元素中使用onsubmit属性,并编写一个JavaScript函数以在出现任何错误时返回false。 I've added fiddle you can try. 我加了小提琴,您可以尝试。

HTML FORM HTML格式

<form action="" method="" onsubmit="return dosubmit();">
  <input type="text" id="name" />
  <input type="email" id="email" />
  <input type="submit" value="Submit" />
</form>

JAVASCRIPT JAVASCRIPT

function dosubmit() {
    if(false) { //Check for errors, if there are errors return false. This will prevent th form being submitted.

    return false;
  } else {
    return true;
  }
}

Let me know if this fixes your issue. 让我知道这是否可以解决您的问题。

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

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