我的父文件中有这个函数:
$(function () {
$('#xybutton').click(function (event) {
event.preventDefault();
window.name = 'parentwindow';
var popupwindow = window.open(
'popup.php',
'',
'toolbar=0,location=0,directories=0,status=1,' +
'menubar=0,scrollbars=yes,resizable=yes,' +
'width=600,' +
'height=400'
);
if(window.focus){
popupwindow.focus();
}
});
});
我的弹出窗口中有一个表单
<form method="post" action="newdoc.php" enctype="multipart/form-data" class="ajax lock-page" id="xyform" name="xyform">
我在相应的js文件中有这个函数:
$(document).ready(function() {
$('#xyform').target = 'parentwindow'; //I also tried window.opener.name
console.log(window.opener.name); //This prints parentwindow
});
但是表单的结果仍然在弹出窗口中打开,而不是在父窗口中打开。 请告诉我哪里出错了。
最佳答案
您正在 jQuery 对象上设置属性 .target
,该属性没有任何意义。
您可能需要的是:
$('#xyform').attr('target', 'parentwindow');
或者,另一种选择:
$('#xyform').get(0).target = 'parentwindow';
后者使用 get
为您提供底层 DOM 对象(准确地说,是本例中的第一个对象 - 无论如何,它是唯一的一个,因为您是通过 ID 选择的)。如果无法通过 jQuery 访问其他属性,这也可用于访问其他属性。
关于javascript - Form.target = window.opener.name 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37055973/