如何在表单提交上运行jQuery函数?

如何在表单提交上运行jQuery函数?,jquery,Jquery,我有一个jQuery函数,希望在表单成功提交后运行该函数。该功能在屏幕溢出时消失,然后在重定向到另一个url之前在列表中显示项目 以下是脚本: <script > function startCheck() { var e = $(".overlay-checker"), t = $(".overlay-checker-points > li"); for (t.hide(), e.fadeIn(), i = 0; i <

我有一个jQuery函数,希望在表单成功提交后运行该函数。该功能在屏幕溢出时消失,然后在重定向到另一个url之前在列表中显示项目

以下是脚本:

    <script >
      function startCheck() {
    var e = $(".overlay-checker"),
        t = $(".overlay-checker-points > li");
    for (t.hide(), e.fadeIn(), i = 0; i < t.length; i++) setTimeout(function() {
        $(".overlay-checker-points").find(":hidden").first().fadeIn()
    }, 1500 * (i + 1));
    setTimeout(function() {
        window.location = $(".redirectNow").attr("href")
    }, 1500 * t.length + 2e3)
    }
   </script>

成功提交表单后,如何运行上述函数?

您可以使用以下侦听器:

$(document).on("submit", "form", function(e){
   // Prevent form submission
   e.preventDefault();
   // Show loader
   // Optional, but showing that something's happening is probably good
   // Assuming you have some element with class "loader" hidden...
   $(".loader").show();
   // Submit form via ajax call
   $.ajax({
      url: "path_to_your_file.php",
      type: "post",
      data: $("form").serialize(),
      success: function(data){
         // Form submitted
         // Call script here 
      },
      error: function(data){
         // Handle error here
      },
      complete: function(){
         // Will be called after success or error
         // Hide loader (if shown before ajax call above)
         $(".loader").hide();
      }
   });
})
在服务器端,您可以使用$\u POST[name]检索表单的输入

编辑:

这是您的HTML表单的外观:

<form>
   <input type="email" name="useremail" placeholder="Email address" required>
   <!-- Add any other input, select, textarea of your liking -->
   <button type="submit">Submit</button>
</form>

可能通过使用提交事件?你现在怎么运行它?发表一篇请帖我对jQuery的了解还不够,无法形成任何接近实际的例子。谢谢@Will。如果我遵循您的示例,那么如何格式化表单html?那么action属性呢?您可以省略action属性,因为表单将通过ajax提交。我正在更新我的回复,以向您展示如何将表格组合在一起。
$email = $_POST["useremail"];