2016-04-06 125 views
1

我使用的是jQuery Validate Plugin,我希望能夠隱藏我的輸入旁邊的錯誤消息,並在底部有一個主要的錯誤消息,我有這種工作類型,但錯誤消息顯示在我的輸入字段旁邊。 (很明顯,如果我使用它,我會清理它的樣式)。如何隱藏錯誤消息並有一個錯誤消息jQuery驗證?

正如你可以在底部看到它告訴我有4個錯誤見下面的細節..我想這留下來,但不是上面的錯誤消息和樣式?

enter image description here

$(".form-group-rules").validate({ 
    rules: { 
     rule_name: { 
      required: true 
     }, 
     rule_desc: { 
      required: false 
     }, 
     rule_type: { 
      required: true 
     }, 
     vali_type: { 
      required: true 
     }, 
     tran_type: { 
      required: true 
     }, 
     vali_fields: { 
      required: true 
     }, 
     acct_sel: { 
      required: true 
     }    
    }, 
    messages: { 
    rule_name: "Please enter a rule name", 
    rule_type: "Please select a rule type", 
    vali_fields: "Please select a validation field", 
    tran_type: "Please select at least 1 transaction type", 
    vali_type: "Please select a valiation type", 
    acct_sel: "Please select at least 1 account" 
    }, 
    showErrors: function(errorMap, errorList) { 
    $(".error-container").html("Your form contains " 
     + this.numberOfInvalids() 
     + " errors, see details below."); 
    this.defaultShowErrors(); 
    }  
}); 
+2

檢查這個問題http://stackoverflow.com/questions/13716649/using-jquery-validate-plugin-to-輸出一個單一的錯誤消息的多個字段 – Pugazh

+0

@Pugazh謝謝你,這是解決方案。 – Kieron606

回答

0

您可以隱藏與CSS的錯誤信息:

span.error { 
    display: none; 
} 
1

可能使用的驗證方法

errorPlacement: function(error,element) { 
    return true; 
    } 

它不會錯誤追加到輸入。

+0

這是不必要的附加代碼,因爲OP通過'this.defaultShowErrors()'以編程方式將這些消息放在適當位置。 – Sparky

0

通常,showErrors將自動禁止每個輸入元素旁邊的默認消息。

您正在創建自己的問題,因爲.defaultShowErrors()是用於放回默認消息的方法。

只需刪除this.defaultShowErrors() ...

showErrors: function(errorMap, errorList) { 
    $(".error-container").html("Your form contains " 
     + this.numberOfInvalids() 
     + " errors, see details below."); 
    this.defaultShowErrors(); // <- REMOVE THIS LINE 
} 

DEMO:http://jsfiddle.net/jmdnxedq/