将变量从页面传递到使用js或jquery的已打开页面
问题描述:
我有一个第一页“main.php”,带有一个打开弹出窗口“pop.php”的按钮,其中可以检查不同选项。在选择不同的选项并按下“ok”后,是否有可能使用js或jquery将“pop.php”表单的结果传递给“main.php”中的字段而不重新加载? 感谢您的回答!将变量从页面传递到使用js或jquery的已打开页面
答
是。有了JavaScript,使用jQuery就变得简单了。
的jQuery上pop.php
<script>
jQuery(function($){
// when your popup form submits this will be called
$('form').submit(function(event){
// this stops the form from submitting
event.preventDefault();
// this sends the form data to a JS function on main.php
window.parent.popup_data_process($(this).serialize());
// this closes this window because we don't need it anymore
window.close();
});
});
</script>
JS上main.php
<script>
// this function is the receiver which will process your form data
function popup_data_process(serializedData)
{
// this displays the data to your console
console.log(serializedData);
}
</script>
答
您可以使用window.parent属性访问父窗口。并调用任何全局级别功能,您希望
var popUp = window.open("pop.php");
内pop.php
if(!!window.parent){
window.parent.someFunction(dataToPadd);
}
谢谢!它帮助很大! – Snipchain 2014-09-05 20:07:50