简体   繁体   English

使用 Javascript,如何在提交后在弹出窗口 window 中显示表单结果?

[英]Using Javascript, how can I display form results in a popup window after being submitted?

I'm fairly new to Javascript.我对 Javascript 相当陌生。 I'm trying to write a pretty basic program that will display the results of a form in a popup window after being submitted by the user.我正在尝试编写一个非常基本的程序,该程序将在用户提交后在弹出窗口 window 中显示表单的结果。

I've got a form with all the relevant IDs.我有一个包含所有相关 ID 的表格。 My problem is accessing this form from within the popup window.我的问题是从弹出窗口 window 中访问此表单。 I've been playing around with window.opener.formid1 , window.parent.formid2 , document.forms[0] changing values where necessary, trying anything my countless searches have returned, but all I seem to get are "undefined" errors.我一直在玩window.opener.formid1window.parent.formid2document.forms[0]在必要时更改值,尝试我无数次搜索返回的任何东西,但我似乎得到的只是“未定义”错误。 I tried defining my variables in several different places but after a lot of testing I'm getting nowhere.我尝试在几个不同的地方定义我的变量,但经过大量测试后我一无所获。

I'm aware you can use, for example我知道你可以使用,例如

var popup = window.open("popup.html")
popup.document.write('')

which works fine but I don't want to overwrite the contents that are already in there and unless I'm mistaken, there isn't a way to append or edit a file.这工作正常,但我不想覆盖已经存在的内容,除非我弄错了,否则没有办法 append 或编辑文件。

If someone can offer a solution or any feedback, it'd be much appreciated.如果有人可以提供解决方案或任何反馈,将不胜感激。 Thanks.谢谢。

Have you tried: popup.document.getElementById('formID').appendChild .您是否尝试过: popup.document.getElementById('formID').appendChild I beleive that should work.我相信这应该有效。

If I understand it correctly, you are trying to submit your form and want to display the result in a new pop up.如果我理解正确,您正在尝试提交表单并希望在新的弹出窗口中显示结果。

If you want to submit your form and want to get the response use the AJAX which is again javascript.如果您想提交表单并希望获得响应,请使用 AJAX,它又是 javascript。 You can get AJAX examples from google for your assistance.您可以从 google 获取 AJAX 示例以获得帮助。

As you have already mentioned, writing on popup or sending value to the popup (may be through query string) would work after you receive the result from AJAX request.正如您已经提到的,在您收到 AJAX 请求的结果后,在弹出窗口上写入或向弹出窗口发送值(可能是通过查询字符串)将起作用。

From the popup, the following should work:从弹出窗口中,以下应该起作用:

var form = window.opener.document.getElementById('formid1');
// Get data from form here.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM