2014-10-18 28 views
0

我有以下代碼,它適用於提交,但我希望valiadtion在onchange或onblur的任何輸入字段更改上運行。onchage驗證onchage不適用於流星j

這不適用於Onchange paramtter。 From https://github.com/DiegoLopesLima/Validate

Template.contactSubmit.rendered = function(){ 


    $('form').validate({ 
     onChange: true, 
     sendForm:false, 
     valid: function() { 
      var post = { 
      email: $('form').find('[name=email]').val(), 
      name: $('form').find('[name=name]').val(), 
      question: $('form').find('[name=question]').val(), 
      found_us: $('form').find('[name=found_us]').val() 

      }; 

      post._id = Contacts.insert(post); 
      Router.go('acorn', post); 
      console.log("valid !"); 
     }, 
     invalid: function(){ 
     console.log("notvalid"); 

     } 

    }); 





} 

回答

1

爲什麼不觸發模板事件的驗證?

Template.contactSubmit.events({ 
    'change form': function(event, template) { 
    validateForm(template.find('form')); 
    } 
}); 


var validateForm = function(form){ 
$(form).validate({ 
     onChange: false, 
     sendForm:false, 
     valid: function() { 
      var post = { 
      email: $(form).find('[name=email]').val(), 
      name: $(form).find('[name=name]').val(), 
      question: $(form).find('[name=question]').val(), 
      found_us: $(form).find('[name=found_us]').val() 

      }; 

      post._id = Contacts.insert(post); 
      Router.go('acorn', post); 
      console.log("valid !"); 
     }, 
     invalid: function(){ 
     console.log("notvalid"); 

     } 

    }); 
} 
+0

當我改變輸入並且所需輸入爲空時,無效函數從不運行。我試圖將onChange設置爲true並沒有工作。它適用於提交,但不適用onChange或onKeyup。 – 2014-10-18 23:44:49