简体   繁体   English

js滚动查看只在提交完成时

[英]Js scroll to view only when the submit complete

Hello I'm trying to scroll into the div only when the submit finishes with promisses您好,我仅在提交完成时才尝试滚动到 div

function submitForm(form) {
    console.log("submitForm: ", form);

    return Promise.resolve(() => form.submit());
}

async function submitFm (form) {
    console.log("submitFm: ", form);
    await submitForm(form);
};

submitButton.addEventListener("click", () => {

            submitFm(form).then(() => {
                console.log("should display after submit is done!");
            });


        });

The promise is suppose to work like this, does anyone knows what's missing ?承诺应该像这样工作,有谁知道缺少什么? Thanks in advance提前致谢

The submit() method on a form triggers form submission (and navigation to the result).表单上的submit()方法触发表单提交(并导航到结果)。

As soon as that is triggered, the submit() function finishes.一旦触发, submit()函数就会完成。

Then the promise resolves.然后承诺解决。

Then the then function executes.然后then函数执行。

Then the browser navigates to the new page.然后浏览器导航到新页面。


There is no way for JavaScript running in a web page to directly cause JavaScript to run in the next page the viewport navigates to.在网页中运行的 JavaScript 无法直接导致 JavaScript 在视口导航到的下一页中运行。

Unloading the current page will kill any JavaScript running in it.卸载当前页面将杀死其中运行的所有 JavaScript。 The next page, whether it be on the other end of a link, or a form submission or a call to location.reload() , etc. is a blank slate.下一页,无论是在链接的另一端,还是表单提交或对location.reload()的调用等,都是一张白纸。 Any JS in that page runs from scratch.该页面中的任何 JS 都是从头开始运行的。

If you want to cause an effect in it you need to do so by passing a message through something that will persist between page loads.如果你想在其中产生影响,你需要通过在页面加载之间持续存在的东西传递消息来实现。

Examples include a query string on the URL or the Session Storage API.示例包括 URL 或会话存储 API 上的查询字符串。

Then you need JavaScript in the next page to check for that message, act on it, and possibly clean it up so it doesn't effect the next page load.然后,您需要在下一页中使用 JavaScript 来检查该消息,对其采取行动,并可能对其进行清理,以免影响下一页的加载。

If you submit a form base on form action, you need to pass the state to the next page.如果您基于表单操作提交表单,则需要将状态传递到下一页。 The simpler way is that use an anchor in the URL, URL with a hashtag will scroll to the element that id is the hashtag.更简单的方法是在 URL 中使用锚点,带有主题标签的 URL 将滚动到 id 是主题标签的元素。

eg例如

<form action="/post/data#id" method="post"></form>

If you submit based on ajax or fetch, you redirect the URL when submitted, you also use hashtags.如果您基于 ajax 或 fetch 提交,则在提交时重定向 URL,您还使用主题标签。 or you can persistence your data by Storage API, and control the scroll action on the next page.或者您可以通过 Storage API 持久化您的数据,并控制下一页的滚动动作。

If you use SPA frameworks like Vue React or others.如果您使用 Vue React 或其他 SPA 框架。 You can use the router API to handle URL changes on submit.您可以使用路由器 API 来处理提交时的 URL 更改。 or handle scroll directly when form submits success.或者当表单提交成功时直接处理滚动。

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

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