我的预期目的是由主窗口弹出一个子窗口,这个子窗口上有一个表单,我想让这个表单提交后,自动关闭这个子窗口,相关代码如下:function addSeed(parmater){
            if(parmater == '2'){
                if (confirm("登记确认后将不能进行任何操作,是否继续?")){
                    document.forms[0].action = "<%=request.getContextPath()%>/product/tjgl.do?method=tj_addimport&savep=" + parmater;
                    document.forms[0].submit();
                    return this.window.close();
                }else{
                    url = "";
                }
            }else{
                document.forms[0].action = "<%=request.getContextPath()%>/product/tjgl.do?method=tj_addimport&savep=" + parmater;
                document.forms[0].submit();
                return this.window.close();
            }
        }表单按钮的onclick事件绑定了这个js函数。
现在的问题是表单似乎没有执行提交就已经关闭了,也就是说,不管到哪个分支,这个函数只执行了return this.window.close(),没有执行其他的代码,请问该如何是好?小弟谢谢大家了!

解决方案 »

  1.   

    js不像java顺序执行
    你可以去掉return this.window.close();这一句,后台处理提交的表单字段之后PrintWriter writer = response.getWriter();
    writer.write("<html><body onload=\"window.close()\"></body></html>");
    return null;或则跳转到一个jsp,jsp的内容类似于上面那样。
      

  2.   

    假设一下,表单如果已经提交了,再关闭窗口,你看到的跟现在看到的有什么不同吗?
    如果不接收从后台传回的对提交的回应,并根据回应来决定后续的显示内容,你看到的本来就该是现在这样。如果要由后台回应来决定前台的后续显示,前台Js的逻辑就不该是这样。应该由后台来决定转向至哪个页面,或者以Ajax来接受后台回应并在回调函数中关闭窗口。
      

  3.   

    return this.window.close(),
    别这样写 
    直接这样写 this.window.close(),
      

  4.   

    这就是说请求发送出去,不要告诉说  你页面上没有设置form 表单哦...............
      

  5.   

    这好办,在close之前加上window.opener的刷新,刷新可以用自己的方法,也可以用js的location.reload
      

  6.   

    谢谢您,我根据您所说的办法,我做了如下改动:假设点击主窗口A上的一个按钮,弹出子窗口B,B执行完之后刷新A窗口,并且焦点转移到A窗口,具体代码改成如下所示:var xmlHttp = false;
            try{
                xmlHttp = new XMLHttpRequest();
            }catch(tryMicrosoft){
                try{
                    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                }catch(otherMicorsoft){
                    try{
                        xmlHttp = new ActiveXObject("Micorsoft.XMLHTTP");
                    }catch(otherChrome){
                        xmlHttp = false;
                    }
                }
            }        var hasClosed;
            function closeWin(){
                hasClosed = true;
                window.opener.location="javascript:reloadPage();";
                window.opener = null;
                window.close();
            }        window.onbeforeunload = function(){
                if(!hasClosed){//如果已经执行了closeWin方法,则不执行本方法
                    window.opener.location="javascript:reloadPage();";
                }
            }        function reloadPage(){
                history.go(0);
                document.execCommand("refresh")
                document.location = document.location;
                document.location.reload();
            }
            
            function addSeed(parmater){
                if(parmater == '2'){
                    if (confirm("登记确认后将不能进行任何操作,是否继续?")){
                        document.forms[0].action = "<%=request.getContextPath()%>/product/tjgl.do?method=tj_addimport&savep=" + parmater;
                        closeWin();
                    }else{
                        url = "";
                    }
                }else{
                    document.forms[0].action = "<%=request.getContextPath()%>/product/tjgl.do?method=tj_addimport&savep=" + parmater;
                    closeWin();
                }
            }
    现在的问题是B窗口确实能关闭,并且表单action也的确执行了,但主窗口A并没有自动刷新,需要手动刷新才行。而且在firebug上调试出现错误,说是reloadPage is not defined,我猜测是因为reloadPage()这个方法的作用域只限于子窗口B,但A似乎调用了这个方法,我不知道在哪里调用了这个方法才导致reloadPage is not defined这个错误,恳请您指点迷津,时间紧急,请多多关照啊,谢谢您。
      

  7.   

    window.opener.location=后面的值应该是一个url串,而你reloadPage方法并没有返回串。
    之所以报出reloadPage is not defined错误,就是因为window.opener.location="javascript:reloadPage();";,相当于A窗口执行window.location="javascript:reloadPage();";,而你A窗口没有reloadPage方法。