2014-06-20 62 views
0

我的表單有兩個提交按鈕,一個提交主窗體,另一個提交使用ajax發送數據。無法弄清楚如何告訴bootstrap驗證程序,在這種情況下我們使用不同的提交按鈕。是的,在提交即時通訊使用preventDefault();使用bootstarp驗證器的目標特定提交按鈕

$("#submitformat").click(function(event) { 
    $('#formid').bootstrapValidator({ // this id is correct 
    message: 'This value is not valid', 
    fields: { 
     alias: { 
      message: 'The username is not valid', 
      validators: { 
       notEmpty: { 
        message: 'The username is required and cannot be empty' 
       }, 
       stringLength: { 
        min: 6, 
        max: 30, 
        message: 'The username must be more than 6 and less than 30 characters long' 
       }, 
       regexp: { 
        regexp: /^[a-zA-Z0-9_]+$/, 
        message: 'The username can only consist of alphabetical, number and underscore' 
       } 
      } 
     }, 
    } 
}); 

HTML按鈕:

<button type="submit" id="submitformat" class="btn btn-primary">Submit</button> 

回答

0

Accordig到Here你必須使用submitHandler選項上bootstrapvalidator阿賈克斯

從鏈接

抽放工藝使用它

使用Ajax提交表單

如果您希望在此之後使用Ajax提交表單並執行其他任務,則可以使用submitHandler選項。

考慮到我們的模態例如,下面的代碼演示如何:

檢查是否有被通過Ajax請求 如果是將它們發送到遠程URL與給定的用戶名和密碼的帳戶,刷新當前頁面 否則,顯示消息通知該帳戶中沒有找到

<script> 
$(document).ready(function() { 
    $('#loginForm').bootstrapValidator({ 
     message: 'This value is not valid', 
     feedbackIcons: { 
      valid: 'glyphicon glyphicon-ok', 
      invalid: 'glyphicon glyphicon-remove', 
      validating: 'glyphicon glyphicon-refresh' 
     }, 
     submitHandler: function(validator, form, submitButton) { 
      $.post(form.attr('action'), form.serialize(), function(result) { 
       // The result is a JSON formatted by your back-end 
       // I assume the format is as following: 
       // { 
       //  valid: true,   // false if the account is not found 
       //  username: 'Username', // null if the account is not found 
       // } 
       if (result.valid == true || result.valid == 'true') { 
        // You can reload the current location 
        window.location.reload(); 

        // Or use Javascript to update your page, such as showing the account name 
        // $('#welcome').html('Hello ' + result.username); 
       } else { 
        // The account is not found 
        // Show the errors 
        $('#errors').html('The account is not found').removeClass('hide'); 

        // Enable the submit buttons 
        $('#loginForm').bootstrapValidator('disableSubmitButtons', false); 
       } 
      }, 'json'); 
     }, 
     fields: { 
      username: { 
       validators: { 
        notEmpty: { 
         message: 'The username is required' 
        } 
       } 
      }, 
      password: { 
       validators: { 
        notEmpty: { 
         message: 'The password is required' 
        } 
       } 
      } 
     } 
    }); 
}); 
</script> 

加上本時並不需要覆蓋。點擊()當按鈕有sumbit(按鈕的類型)

0

Validator可能不關心表單是如何提交的。您可能需要這樣做來檢測點擊了哪個按鈕:

if (event.target == $('#submitformat')) { 
    ... 
}