Reseting the form when usering the jquery validations plugin

你。 提交于 2020-01-02 05:37:13

问题


I have a simple form like below that I have added the jQuery validations plugin onto (http://docs.jquery.com/Plugins/Validation). I have this form in a modal popup window so if there are errors and the user closes the window when they open it again the form still has the errors. In my popup close callback I tried calling resetForm() but it says the method doesn't exist.

Form HTML:

 <form class="validations" id="commentForm" method="get" action="">
   <p>
     <label for="name">Name</label>
     <em>*</em><input id="name" name="name" size="25" class="required" minlength="2" />
   </p>
   <p>
     <label for="email">E-Mail</label>
     <em>*</em><input id="email" name="email" size="25"  class="required email" />
   </p>
 </form>

Popup Close Callback:

function(){
  $(this).find('form.validations').resetForm();
}

Thanks in advance for the help.


回答1:


resetForm is part of the object returned by the validate method, not the form. Example:

var validate = $('#commentForm').validate({ ... });
// Later...
validate.resetForm();
// Or if variable scope is in the way...
$('#commentForm').data('validator').resetForm();

The validation plugin stores a reference to the validation object in the form's data store.




回答2:


If it really is a short form with just a few elements, you could simply reset them by hand when the close button on the modal window is clicked, like so:

$("input[name='formelementName']").val("");



来源:https://stackoverflow.com/questions/2060447/reseting-the-form-when-usering-the-jquery-validations-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!