Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript、onbeforeunload事件在IE中提交表单时起作用_Javascript - Fatal编程技术网

Javascript、onbeforeunload事件在IE中提交表单时起作用

Javascript、onbeforeunload事件在IE中提交表单时起作用,javascript,Javascript,我在为xhtml页面制作的javascript上为adivce写文章。我让它主要用于IE、FF和chrome(Safari仍然让我有些悲伤,但这是另一个主题)。我遇到的问题是,即使有人提交表单,IE也会运行beforeunload事件,这不是我希望发生的事情,因为beforeunload脚本会导致信息被重置。它甚至在我按下F5键时也会这样做,这也不是我想要的。现在FF和Chrome在提交表单时没有这个问题,但我不知道为什么。这是我到目前为止编写的代码 <script type="text/

我在为xhtml页面制作的javascript上为adivce写文章。我让它主要用于IE、FF和chrome(Safari仍然让我有些悲伤,但这是另一个主题)。我遇到的问题是,即使有人提交表单,IE也会运行beforeunload事件,这不是我希望发生的事情,因为beforeunload脚本会导致信息被重置。它甚至在我按下F5键时也会这样做,这也不是我想要的。现在FF和Chrome在提交表单时没有这个问题,但我不知道为什么。这是我到目前为止编写的代码

<script type="text/javascript"> 
window.onbeforeunload = function() {
var hiddenBtn = document.getElementById("kioskform:broswerCloseSubmit");
return  hiddenBtn.click();
           }   </script>

window.onbeforeunload=函数(){
var hiddenBtn=document.getElementById(“kioskform:broswerCloseSubmit”);
返回hiddenBtn.click();
}   

我只是想知道怎么做,非常感谢你的建议

定义一个html隐藏变量。在用户提交表单时为指定值。然后在unload函数中,检查隐藏变量。如果它填充了任何值,则保留该函数

<script type="text/javascript"> 
 window.onbeforeunload = function() {
  if (document.getElementById("wasformsubmitted").value != "Y") {
    var hiddenBtn = document.getElementById("kioskform:broswerCloseSubmit");
    return  hiddenBtn.click();
  }
}   
function SetSubmitVariables()
{
 document.getElementById("wasformsubmitted").value = "Y";
 return true;
}
</script>
<body>
  <form ... onsubmit="return SetSubmitVariables();">
    <input type="hidden" id="wasformsubmitted" value="N" />
    ... 
  </form>
</body>

window.onbeforeunload=函数(){
if(document.getElementById(“wasformsubmitted”).value!=“Y”){
var hiddenBtn=document.getElementById(“kioskform:broswerCloseSubmit”);
返回hiddenBtn.click();
}
}   
函数SetSubmitVariables()
{
document.getElementById(“wasformsubmitted”).value=“Y”;
返回true;
}
... 

定义html隐藏变量。在用户提交表单时为指定值。然后在unload函数中,检查隐藏变量。如果它填充了任何值,则保留该函数

<script type="text/javascript"> 
 window.onbeforeunload = function() {
  if (document.getElementById("wasformsubmitted").value != "Y") {
    var hiddenBtn = document.getElementById("kioskform:broswerCloseSubmit");
    return  hiddenBtn.click();
  }
}   
function SetSubmitVariables()
{
 document.getElementById("wasformsubmitted").value = "Y";
 return true;
}
</script>
<body>
  <form ... onsubmit="return SetSubmitVariables();">
    <input type="hidden" id="wasformsubmitted" value="N" />
    ... 
  </form>
</body>

window.onbeforeunload=函数(){
if(document.getElementById(“wasformsubmitted”).value!=“Y”){
var hiddenBtn=document.getElementById(“kioskform:broswerCloseSubmit”);
返回hiddenBtn.click();
}
}   
函数SetSubmitVariables()
{
document.getElementById(“wasformsubmitted”).value=“Y”;
返回true;
}
... 

您可以在提交表单时从窗口中删除onBeforeUnload listener

例如,如果您使用id=“form1”提交表单,则可以添加此代码。

document.getElementById('form1')。onsubmit=function(){
onbeforeunload=函数(){};
}

您可以在提交表单时从窗口中删除onBeforeUnload listener

例如,如果您使用id=“form1”提交表单,则可以添加此代码。

document.getElementById('form1')。onsubmit=function(){
onbeforeunload=函数(){};
}

听起来不错,如果可能的话,你能给我举个例子供参考吗?听起来不错,如果可能的话,你能给我举个例子供参考吗?所以我只是把它添加到脚本中,并添加表单ID,这样在调用submit时它就可以忽略onbeforeunload函数了?是这样吗?是的。它对我有用。例如,您可以按“Run”键触发onBeforeUnload事件。是否有这样一个版本,该脚本仅在IE或FireFox浏览器上运行?因此,我只是将其添加到脚本中,并添加表单ID,以便在调用submit时忽略onBeforeUnload函数?是这样吗?是的。它对我有用。例如,您可以按“Run”键触发onBeforeUnload事件。是否有这样一个版本,该脚本仅在IE或FireFox浏览器上运行?