duanhai7274 2013-09-27 00:27
浏览 94
已采纳

使用Javascript关闭窗口时创建事件

I'm creating a popup that will allow me to select values from it, then after I've selected the values, I need to close the window, then pass it to my main form.

The code in my main form:

onClick="cust=window.open('popup.php?count=<?=$RowStart?>&name_id=' + document.form1.name_id<?=$RowStart?>.value'custWindow','top=50, left=50, height=150, width=375, scrollbars=yes, resizable=yes');cust.focus(););

And the code inside the popup:

<form name="form1" action="<?=$_SERVER['PHP_SELF']?>" method="post">
<table width="100%" border="0" cellpadding="0" cellspacing="0"> 
<tr>
<td>
<input id="name" name="name" type="text"/>
<input id="nameid" name="nameid" type="hidden" value ="<?=$name_id?>"/>
</td>
<td>
<input name="ellipse" type="button" value="..." onClick="ellipse=window.open('engine_item_list2.php?name='+document.form1.name.value,'ellipse','top=50,left=50,width=500,height=500, scrollbars=yes, resizable=yes'); ellipse.focus();"/> 
</td>
</tr>
</table>
</form>

What happens is when I click the button in the main form, it goes to open a new window where the popup has a form. Inside the form is an ellipse that gets the value for the name_id using the input from the name textfield.

I am able to get the values from the ellipse to the name_id, because the ellipse has a submit button inside it. But my popup window form1 should not have a save button involved, so I have to pass the values from the popup window back to the main form by either submitting it when I close the popup or using another way.

I think I should use a window.close() but I dont know how to pass the data back using it. Is there a way I can link the close button in the window such that when it gets clicked the values gets to be passed back to the main form?

  • 写回答

2条回答 默认 最新

  • dpwdsmbvm496180204 2013-09-27 00:30
    关注

    When a "done" button or something like that is clicked in your popup window, you should be able to use window.opener from the popup window to find the window that opened it and then put your data into the DOM or javascript variables of that window. After putting the data from the popup into the original window, you can then close the popup with window.close().

    Note, it is more common these days to use a temporary overlay in the same window for your form rather than popping up a new window and it also avoids any conflicts with popup blockers that might get in the way of popping up your new window.

    This will not handle the close corner in the window. That would generally just be equivalent to the "Cancel" operation so nothing needs to be done when it is clicked (per normal UI convention). The typical UI would involve a "Done", "Finished" or "OK" button that would be clicked when the user wants the data entered to be accepted. It would be in the click handler for this button that you would use window.opener to find the parent/main window, put the data from the popup form into that window and then call window.close() to close the popup.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题