2014-03-06 136 views
0

我寫這篇文章code..but我想孩子的時候窗口打開未提交父窗口的形式在子窗口被打開

<script language="javascript"> 
function popwindow(page) { 
OpenWin = this.open(page,"CtrlWindow","top=80,left=100,screenX=100,screenY=80,width=600,height=400,status=yes,toolbar=no,menubar=no,location=no, scrollbars=yes,resizable=yes"); 
OpenWin.focus(); 
return false; 
} 
</script> 

回答

0

做一個全局變量與假,當不提交父窗口的形式你打開孩子/新窗口將其設置爲true。在窗口關閉設置該變量再次爲false。在表單上提交檢查變量狀態。如果是虛假的,請提交,否則不要提交。爲此,使用window.open/window.close方法。

使用此代碼:

HTML

<form method="get" action=""> 
    <button value="open" id="open">Open</button> 
    <input type="submit" value="submit" /> 
</form> 

JS(jQuery的1.9)

$(function(){ 
    var customWindow = false; 
    $('#open').click(function(e){ 
     e.preventDefault(); 
     customWindow = true; 
     customWindow = window.open('http://www.google.com', "","menubar=1,resizable=1"); 
     customWindow.focus(); 
    }); 

    $('[type="submit"]').on('click', function(e){ 
     e.preventDefault(); 
     if(customWindow.closed || !customWindow) { 
      alert('form submit'); 
     } else { 
      alert('please close the child window'); 
     } 
    }); 
}); 

see this fiddle

+0

如何賦值變量? – user3359065

+0

不應該,要麼讀作cusWindow = window.open(...'或'customWindow.focus()'? – scenia

+0

@scenia感謝。代碼更新:) –