验证失败时禁用按钮


Disable button when validation fails

>我有一个表单,它有一个提交按钮,我想禁用提交按钮,直到所有验证都为真。

目前,当一个验证为真时,按钮将启用。(甚至其他字段也有红色错误消息。

<script>           
     $(document).ready(function() {
                    $('[data-toggle="tooltip"]').tooltip();
                    $('#payment-form').formValidation({
                      framework: 'bootstrap',
                      trigger: 'change',
                      fields: {
                        first_name: {
                          validators: {
                            notEmpty: {
                              message: 'The First Name field is required and cannot be empty'
                            }
                            // regexp: {
                            // regexp: /^[a-z''s]+$/i,
                            // message: 'The first name field can consist of alphabetical characters and spaces only'
                            // }
                          }
                        },
                        last_name: {
                          validators: {
                            notEmpty: {
                              message: 'The Last Name is required and cannot be empty'
                            }
                            // regexp: {
                            // regexp: /^[a-z''s]+$/i,
                            // message: 'The Last name can consist of alphabetical characters and spaces only'
                            // }
                          }
                        },
    }
                      });

                 }).on('success.form.fv', function (e) {
                  $('#continue_btn').attr('disabled','false');
                   e.preventDefault();
                    });
                    var i = 0;
                    var l=0;
                    $('#zip_code').keydown(function()
                    {
                      var country_check = $('#country').val();
                      if(country_check=='India')
                      {
                        $('#zip_code').attr('maxlength', '6');
                      }
                      else
                      {
                        $('#zip_code').attr('maxlength', '10');
                      }
                    });
</script>

怎么做?

formValidation中设置下面的属性,如图所示,您可以在表单设置中看到:http://formvalidation.io/settings/

$('#data_form').formValidation({
...
   button: {
       selector: "#id_submit_btn",
       disabled: "disabled"
   }
....
});

您可以在提交按钮上使用toggle

$('button[data-toggle]').on('click', function() {
    var $target = $($(this).attr('data-toggle'));
    $target.toggle();
    if (!$target.is(':visible')) {                         
        $('#payment-form').data('formValidation').disableSubmitButtons(false);
    }
});