我在父窗口中有一个FORM,
<form id="myform">
<username>
<password>
</form>
和jQuery
$("#myform").submit(function(e){
e.preventDefault(); // prevent default form submit
//some of my other codes ...
});
我正在使用window.open
打开一个新窗口
var popupWindow = window.open("myUrl");
在弹出窗口中,我尝试提交myform
表单
$(window.opener.document.getElementById("myform")).submit();
表单已提交,但问题是它使用默认表单提交而不是我附加的事件。
我做错了什么?
请帮帮我。 感谢。
答案 0 :(得分:1)
这就是我的工作:
Parent.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<input type="button" value="Try Me..." onclick='window.open("Child.html");' />
<form action="post.html" method="post" id="myForm">
</form>
</body>
</html>
Child.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script>
function submitParent() {
window.opener.document.forms["myForm"].submit();
window.close();
}
</script>
</head>
<body>
<input type="button" onclick="submitParent();" value="Submit Parent" />
</body>
</html>
单击“提交父级”时,它会关闭子级,并将父级提交给“Post.html”。
祝你好运!
答案 1 :(得分:1)
我明白了。
我意识到如果事件是从其他一些资源触发的,那么通过jQuery附加的事件就无法正常工作。
但它适用于带有内联事件的pure-js,
<form id="myform" onsubmit=" process(); "></form>
并且效果很好。
感谢。
答案 2 :(得分:-1)
尝试这样的事情
<div id="divform">
<form action="/system/wpacert" method="post" enctype="multipart/form-data" name="certform">
<div>Certificate 1: <input type="file" name="cert1"/></div>
<div>Certificate 2: <input type="file" name="cert2"/></div>
<div><input type="button" value="Upload" onclick="closeSelf();"/></div>
</form>
</div>
<div id="closelink" style="display:none">
<a href="javascript:window.close()">Click Here to Close this Page</a>
</div>
function closeSelf(){
document.forms['certform'].submit();
hide(document.getElementById('divform'));
unHide(document.getElementById('closelink'));
}
答案 3 :(得分:-1)
使用window.open
时,您将打开一个新窗口。这开始了一个新线程。您无权访问相同的DOM上下文以及其中包含的元素和事件。