2016-04-22 126 views
1

我有一個表單有一個提交按鈕,0​​我想禁用提交按鈕,直到所有驗證都爲真。驗證失敗時禁用按鈕

當一個驗證變爲true時,按鈕獲取啓用。 (甚至其他領域有着紅色的錯誤消息。)

<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> 

如何做到這一點?

回答

0

低於設定在formValidation屬性,在這裏你可以在形式設置,請參見:http://formvalidation.io/settings/

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

您可以在提交按鈕使用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); 
    } 
}); 
+0

我認爲問題是圍繞着如何陷阱,如果所有的字段都有效,或者如果任何字段無效 –

+0

@JigarJoshi是的,但如何做到文檔加載後立即驗證輸入的事件?或者在提交事件觸發之前。這將在第一次嘗試提交後禁用按鈕,直到所有輸入有效。 –