使用 strutst html 标签时,如何在提交按钮上调用 onclick 事件?

While using strutst html tags, how can I call onclick event on submit button?

我有父页面,其中有一个元素。该元素弹出 window 的点击来了。在弹出 window 上有一个文本框和提交按钮。现在我想在单击弹出 window 上的提交按钮时刷新父页面。 我有刷新父页面的解决方案:

window.onload = function()
{
  window.opener.location.reload();
}

我已经在 jsp 弹出页面中添加了这个脚本。所以每当我点击父页面上的元素时,弹出窗口 window 就会出现并且父页面被刷新(想跳过这个),然后我在弹出窗口的文本框中添加一些文本并提交数据,因为它再次弹出提交按钮重新加载并且父页面被刷新(这是预期发生的)。由于我在弹出页面上使用 window.onload,它会刷新两次,我想跳过第一次刷新。 所以我决定在点击提交按钮时执行刷新。 弹出页面JSP代码:

<td align="center">
 <html:submit styleId="btn_add" property="submitButton" onclick="formConfirmOff();" value="Add" />
</td>  

我试图跳过第一次刷新的另一个解决方案:

document.getElementById("btn_add").onclick = function(){
        window.opener.location.reload();
    }

但是使用上面的脚本它给我的错误是 cannot set 属性 onclick of null 所以这可能是因为脚本在 DOM 加载之前被执行。所以我在

中添加了脚本
window.onload = function(){
        document.getElementById("btn_add").onclick = function(){
            window.opener.location.reload();
        }
    }

但是这什么时候显示带有选项的“确认导航”对话框离开此页面并留在此页面上。 可能是什么问题。传递给 onclick 的函数顺序是否重要? 我只是想在使用提交按钮在弹出子页面上添加数据时刷新父页面。

编辑:

$(document).ready(function() {
        $("#frm_addSpeedData").submit(function(event) {  
        //event.preventDefault();
        $.ajax({
             type: "POST", //chose your request type method POST or GET
              url: "/webapp/addSpeedDataAction.do", // your struts action url
              data: $(this).serialize(),
              success: function() {
                window.opener.location.reload(); // callback code here
              }
            })
        })
    });

此代码有时有效但有时无效。 意味着当弹出窗口打开时,我在弹出窗口上提交数据。然后在成功父 window 得到刷新。但我仍然看不到父页面上的更新。 可能是什么问题?

您需要使用 AJAX 调用您的服务器手动执行此操作,成功执行后您将刷新您的父页面。

下面是使用 JQuery Ajax 的例子:

<script type="text/javascript">
$(document).ready(function() {
  $("#form_selector").submit(function(event) {  
        event.preventDefault();
        $.ajax({
             type: "POST", //chose your request type method POST or GET
              url: "Your_Action", // your struts action url
              data: $(this).serialize(),
              success: function() {
                window.opener.location.reload(); // callback code here
              }
        })
   })
});
</script>  

这是 JavaScript 版本:

var url = "/webfdms/addSpeedDataAction.do";

xmlHttp.open("GET", theUrl, true);
xmlHttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xmlHttp.overrideMimeType("application/octet-stream");
xmlHttp.responseType = "blob";

xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == xmlHttp.DONE) {
        if (xmlHttp.status == 200) {
            window.opener.location.reload();
        }
    }   
};
xmlHttp.send();