我正在尝试使用POST和参数将网页打开到iframe中。
但是,在进行提交时,将重新加载父页面,并且该URL未在iframe中打开:
<head>
<title>iframe Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
iframe {
display: block; /* iframes are inline by default */
background: #000;
border: none; /* Reset default border */
height: 90vh; /* Viewport-relative units */
width: 90vw;
}
</style>
</head>
<body>
<form id="myForm" method="post" action= "http://127.0.0.1/myWeb" target="myIframe">
<input type = "hidden" name="param1" value="param1value">
<input type = "hidden" name="param2" value= "param1value">
</form>
<a href="" onClick="postLogin();" >Send to iframe</a><br />
<script laguaje="javascript">
function postLogin() {
var form = document.getElementById("myForm");
form.submit();
}
</script>
<iframe src="" name="myIframe" id="myIframe">
<p>iframes are not supported by your browser.</p>
</iframe>
</body>
</html>
最佳答案
刷新页面的人是锚标记。您可以使用event.preventDefault()
。
更改添加事件参数的锚标记。
<a href="" onClick="postLogin(event);" >Send to iframe</a>
然后在Javascript函数
postLogin
中添加preventDefault()
事件方法: function postLogin(event) {
var form = document.getElementById("myForm");
form.submit();
event.preventDefault();
}
编辑:
或者,您可以使用不带
<button>
功能的postLogin
而不是<a>
。 <form id="myForm" method="post" action= "http://127.0.0.1/myWeb" target="myIframe">
<input type = "hidden" name="param1" value="param1value">
<input type = "hidden" name="param2" value= "param1value">
<button type="submit">Send to iframe</button>
</form>
关于javascript - 使用JavaScript将表单提交到iframe目标中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37208408/