gpt4 book ai didi

Javascript 表单提交有时不起作用

转载 作者:行者123 更新时间:2023-12-04 19:57:28 25 4
gpt4 key购买 nike

我在 javascript 的 beforeunload 事件期间提交我的表单。对于所有浏览器,它的行为方式都不同。在 chrome 29 中,如果我在 1 分钟内关闭我的浏览器,那么它会触发服务器端操作,但如果我的应用程序空闲超过 1 分钟然后我关闭浏览器,那么它不会触发服务器端操作。同样在 IE10 和 FF20 中,我不能闲置超过 5 分钟。

<script> 
function updateServerCMIModel(item) {
document.forms[0].itemID.value = item;
formstring = top.window.CPFrame.showCurrentModelState("form");
document.forms[0].data.value = formstring;
document.forms[0].nextAction.value = top.window.frames.CPFrame.CPAPI.userEvent;
document.forms[0].lmsAction.value = "update";
document.forms[0].submit();
}
</script>

有什么办法可以解决这个问题吗?

最佳答案

这是一个时间问题。您需要一种方法来延迟页面的实际卸载,直到表单发布成功。一个非常典型的 hack 是在发布表单后向用户抛出一个对话(“你确定要离开这个页面吗?”)并希望用户在点击之前花几秒钟。

为此,只需从事件处理程序返回一个包含您选择的消息的字符串,例如

window.onbeforeunload = function(e) {
doFormPost();
return 'Dialog text here.';
};

关于Javascript 表单提交有时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18917862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com