简体   繁体   English

结合两个javascript函数来验证表单并通过ajax提交

[英]Combining two javascript functions to validating a form and submit it via ajax

I am trying to add validation to a form that is submitted by ajax, and have it partially working. 我试图将验证添加到由ajax提交的表单中,并使其部分起作用。

Here's the code that submits the form: 这是提交表单的代码:

<script type="text/javascript">
$(document).ready(function(){$(".sendName").click(function(){
var Email=$("#Email").val();
var Name=$("#Name").val();
var ErrorType=$("input:radio[name=ErrorType]:checked").val();
var Univer=$("#Univer").val();
var ErrorMessage=$("#ErrorMessage").val();
$.post("report.php",{Email:Email,Name:Name,ErrorType:ErrorType,Univer:Univer,ErrorMessage:ErrorMessage},function(data){$("#loadName").html(data).effect('bounce', { times: 5, distance: 30 }, 300);
  });
 });
});
</script>

That code works perfectly. 该代码运行完美。

I am also using the livevalidation plugin, and have it mostly working as well. 我也在使用livevalidation插件,并且大多数情况下也能正常工作。 The only problem I am having is calling the validation code from within the function that submits the form. 我唯一的问题是从提交表单的函数中调用验证代码。

Normally the validation code is called like this: 通常,验证代码如下所示:

 <form onsubmit="return validate(this)">

However, because the form is being submitted by ajax the current form tag is: 但是,由于表单是由ajax提交的,因此当前的表单标签为:

 <form onSubmit="return false;">

So what I need to be able to do is call the validate function from within the function that submits the form via ajax. 因此,我需要做的是从通过ajax提交表单的函数中调用validate函数。

How would I do something like this? 我该怎么做? Both scripts are working fine on their own, it's just this one last thing I need to figure out. 这两个脚本都可以正常工作,这只是我需要弄清的最后一件事。

So I need to call the validate function when the ajax function is called, and if the form validates the script can complete the ajax request, and if the form does not validate it should not be submitted. 因此,我需要在调用ajax函数时调用validate函数,并且如果表单验证了脚本可以完成ajax请求,并且如果表单未验证,则不应提交。

I did some searching and found a couple similar questions, but no solutions. 我进行了一些搜索,发现了两个类似的问题,但没有解决方案。 Any help would be appreciated. 任何帮助,将不胜感激。

The easier way to do this is to only cancel the submit if there is a validation error, otherwise let the submit happen normally. 更简单的方法是仅在出现验证错误时才取消提交,否则请使提交正常进行。

<form id="new-item" method="post" action="/i">

Then in your javascript: 然后在您的JavaScript中:

$('#new-item').submit( function () {

  var valid = true;

  // do you validation logic
  valid = validate(this);

  // returning true will let the normal submit action happen
  // returning false will prevent the default action (i.e. the form will not be sumitted)
  return valid;

});

If you don't want to use the standard submit functionality (though that is a best practice since it works when javascript is disabled) you would do a similar thing but just always return false. 如果您不想使用标准的提交功能(尽管这是最佳做法,因为它在禁用javascript时可以工作),您可以做类似的事情,但总是返回false。

$('#new-item').submit( function () {

  var valid = true;

  // do you validation logic
  valid = validate(this);

  if (valid ) {
    // send your ajax post request here
  }


  // returning true will let the normal submit action happen
  // returning false will prevent the default action (i.e. the form will not be sumitted)
  return false;

});

Agreeing with bill 同意账单

Assumning this form 假设这种形式

<form id="formId" ...
  .
  .
  <input type="submit" />
</form>

Using your code: 使用您的代码:

<script type="text/javascript">
$(document).ready(function(){
  $("#formId").submit(function(){
    if (!validate(this)) return false;
    var Email=$("#Email").val();
    var Name=$("#Name").val();
    var ErrorType=$("input:radio[name=ErrorType]:checked").val();
    var Univer=$("#Univer").val();
    var ErrorMessage=$("#ErrorMessage").val();
    $.post("report.php",
      {Email:Email,Name:Name,ErrorType:ErrorType,Univer:Univer,ErrorMessage:ErrorMessage},function(data){$("#loadName").html(data).effect('bounce', { times: 5, distance: 30 }, 300);
    });
    return false; // cancel normal submission
   });
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM