使用jQuery中的Validation插件提交后无法清除表单

时间:2022-11-28 14:25:44

I an quite new to jQuery and I have a problem while trying to create a form. I am using the Validation plugin for validate the email (one the form's field). When I click the Submit button I want to call my own function because I want to save the data in an XML file.

我是jQuery的新手,我在尝试创建表单时遇到了问题。我正在使用Validation插件来验证电子邮件(表单的一个字段)。当我单击“提交”按钮时,我想调用自己的函数,因为我想将数据保存在XML文件中。

This is my button: (as I understood the plugin uses "submit" for understand the button)

这是我的按钮:(据我所知,插件使用“提交”来理解按钮)

<input type="submit" name="submit" class="submit" id="submit_btn" value="Send"/>

and here is the script for the validation:

这是验证的脚本:

 <script type="text/javascript">
  $(document).ready(function() {

        //this is my form
 $("#contactForm").validate();

 /*save the valid data in the xml*/
 $(".submit").click(function() { 
 var email = $("input#email").val();
 var subject = $("input#subject").val();
 var message = $("textarea#message").val();

 if (email == "" || !$("#contactForm").valid()) {
   return false;
  }    

 var dataString = 'email='+ email + '&subject=' + subject + '&message=' + message;
 //alert("DATA: " +dataString);  

  $.ajax({
     type: "POST",
     url: "SaveData.jsp",
     data: dataString,  
     success: function(data){}   
  });

  return false;
});
});
</script>

In general it works ok but I have two basic problems. When I click the button in the beginning having all the form empty, I get no message for the field required. Also when the data are valid and I am doing the submit, the form does not become clear after the submit.

一般来说它工作正常,但我有两个基本问题。当我点击开头的所有表格为空的按钮时,我没有得到所需字段的消息。此外,当数据有效且我正在进行提交时,表单在提交后不会变得清晰。

If I deleted this script code, these actions are working properly but I can not save the data!

如果我删除了此脚本代码,这些操作正常,但我无法保存数据!

Does anyone know what is wrong?

有谁知道什么是错的?

Thanks a lot!

非常感谢!


I found something finally. I changed the line:

我终于找到了一些东西我换了一行:

$(".submit").click(function() {....}

with this one:

与这一个:

submitHandler: function() { ....}

So now when I click the button I get the right message for the required field. The only thing I haven't found yet is how to clear the form after submitting. I know how to do this manually in an other function, but I saw that the Plugin does this thing also. So I was thinking that probably is better to use the one the Plugin has.

所以现在当我点击按钮时,我得到了所需字段的正确消息。我还没有发现的唯一一件事就是如何在提交后清除表格。我知道如何在其他函数中手动执行此操作,但我看到插件也执行此操作。所以我认为使用插件所具有的可能更好。

So does anyone know what it wrong?

那么有谁知道它错了吗?

Thanks

谢谢

1 个解决方案

#1


1  

First, store a reference to your validator, like this:

首先,存储对验证器的引用,如下所示:

var validator =  $("#contactForm").validate();

In your $.ajax success handler, or elsewhere if you want, you can call this:

在你的$ .ajax成功处理程序中,或者你想要的其他地方,你可以这样称呼:

validator.resetForm();

You can read the docs for .resetForm() here

您可以在此处阅读.resetForm()的文档

#1


1  

First, store a reference to your validator, like this:

首先,存储对验证器的引用,如下所示:

var validator =  $("#contactForm").validate();

In your $.ajax success handler, or elsewhere if you want, you can call this:

在你的$ .ajax成功处理程序中,或者你想要的其他地方,你可以这样称呼:

validator.resetForm();

You can read the docs for .resetForm() here

您可以在此处阅读.resetForm()的文档