调用 form.submit() 方法后的 javascript 连续

发布于 2025-01-16 08:57:55 字数 1594 浏览 2 评论 0原文

当我网站上的 Asp.Net aspx 页面加载时,Javascript 脚本会检查某些条件,如果满足该条件,它应该以编程方式设置输入元素的值,然后提交表单,也可以通过编程方式使用 form.submit()。这样,我就没有使用 clicksubmit 事件。表单应该在后端处理,然后页面重新加载正确的数据(并且它正确地执行了此操作)。

由于某种原因,这似乎在 FireFox 中工作正常,但在 Chrome 中表现不同。

Firefox 中的作用是,在 form.submit() 之后,页面会重新加载,并且我的 Javascript 会按照预期从头开始(再次运行检查)。

然而,在 Chrome 上,似乎在 form.submit() 调用之后,Javascript 只是继续剩余的 Javascript 代码。 我尝试了 form.requestSubmit(),但是但这并没有改变它。

我的代码:

// check if someCondition is true, if so, a form field needs to be set prgrammatically and then the form submitted. If condition is false, just continue
if(someCondition){
   VerifyBeforeCheck();
}

DoTheNextThingIfConditionIsFalse(); //In Firefox, if VerifyBeforeCheck() is fired/ reached, this code never gets executed. In Chrome it will, but I don't want that.

// simple wrapper function, because SubmitMyForm gets called from different events and methods.
function VerifyBeforeCheck() {
    let newValue = sessionStorage.getItem("ItemFromSessionStorage");
    let fld = document.getElementById("formInputField"); // grabs an <input> field
    field.value = newValue; // used in backend
    SubmitMyForm();
}


function SubmitMyform(){
    let form = document.getElementById('myForm');
    form.submit();
}

这确实是意外行为还是我错过了什么?我希望 Chrome 中的情况与当前在 Firefox 中运行时的情况一样:在提交方法之后,提交表单,然后重新加载页面。如果我需要使用 PreventDefault,在没有事件的情况下我该如何做到这一点?

When a Asp.Net aspx page on my site loads, a Javascript script runs a check on some condition, and if that condition is met, it should set an input element's value programmatically, and then submit the form, also programmatically, using form.submit(). With this, I am not using a click or submit event. The form should be processed backend and then the page reloaded with the correct data (and it does this correctly).

For some reason, this seems to work fine in FireFox, but it behaves differently in Chrome.

What is does in Firefox is that after the form.submit() the page reloads and my Javascript gets kicked off from the start (running the check again), as expected.

However, on Chrome, it seems that after the form.submit() call, the Javascript just continuous with the remaining Javascript code.
I tried the form.requestSubmit(), but that didn't change it.

My code:

// check if someCondition is true, if so, a form field needs to be set prgrammatically and then the form submitted. If condition is false, just continue
if(someCondition){
   VerifyBeforeCheck();
}

DoTheNextThingIfConditionIsFalse(); //In Firefox, if VerifyBeforeCheck() is fired/ reached, this code never gets executed. In Chrome it will, but I don't want that.

// simple wrapper function, because SubmitMyForm gets called from different events and methods.
function VerifyBeforeCheck() {
    let newValue = sessionStorage.getItem("ItemFromSessionStorage");
    let fld = document.getElementById("formInputField"); // grabs an <input> field
    field.value = newValue; // used in backend
    SubmitMyForm();
}


function SubmitMyform(){
    let form = document.getElementById('myForm');
    form.submit();
}

Is this indeed unexpected behavior or am I missing something? I want the situation in Chrome to be as it is currently when I run it in Firefox: After the submit method, the form is submitted and then a page reload. If I need to use a preventDefault, how can I do this, without the event?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文