简体   繁体   English

提交完成后如何执行代码?

[英]How to execute code only after the submission is done?

  1. I have some code from a service called webflow where the form is controlled by them.我有一些来自名为 webflow 的服务的代码,其中表单由它们控制。 I write the html form, but the form submission code is handled by a third party not me我写了html表单,但是表单提交代码是由第三方而不是我处理的
  2. I need to run some code (trigger a redirect) after their submission is done.我需要在提交完成后运行一些代码(触发重定向)。

Here is my attempt这是我的尝试

$('#email-form').submit(function (event) {      
    window.location.href = url;
});

It works in some browser, but not in safari.它适用于某些浏览器,但不适用于 safari。 Apparently, this breaks this original form submission code.显然,这破坏了原始表单提交代码。

My second attempt, this works我的第二次尝试,这有效

    $('#email-form').submit(function (event) {      
    setTimeout(function(){
    window.location.href = url;
    }, 500);

});

By giving it some time, the submission code fires first, then my code runs and successfully redirects.通过给它一些时间,提交代码首先触发,然后我的代码运行并成功重定向。

What is a more robust way of writing this code (not reliant on some random timer wait)?编写此代码的更健壮的方法是什么(不依赖于一些随机计时器等待)?

我认为在每个浏览器中工作的最佳方式是👇

setTimeout(function(){document.location.href = "your link";},250);

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

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