提问人:apex anchor 提问时间:11/18/2022 更新时间:11/18/2022 访问量:99
使用 iframe 外部的按钮提交 iframe 表单
Submit iframe forms with button outside of iframe
问:
我有 contactus.html它的 iframe 只包含三个输入contact_inputs.php<iframe name="myframe" id="frame1" src="contact_inputs.php"></iframe>
和 ContactUs 中的按钮提交.html我想在 iframe 文件中提交表单contact_inputs.php使用 iframe 外部的按钮提交给php_form.php
联系我们:.html
<iframe name="myframe" id="frame1" src="contact_inputs.php"></iframe>
<form action="php_form.php" method="post">
<input type="submit" name="DoIt" value="submit">
</form>
contact_inputs.php
<form>
<input type="" name="cn">
<input type="" name="ex">
<input type="" name="cr">
</form>
答:
0赞
Jerry
11/18/2022
#1
在这里你可以试试这个逻辑:
function sub(ev) {
ev.preventDefault();
let frame1 = document.getElementById("frame1");
let parser = new DOMParser();
let doc = parser.parseFromString(frame1.innerHTML, "text/html");
let iframeForm = doc.body.querySelector("form");
console.log(iframeForm);
//access value of form input field via name also
console.log(iframeForm.cn.value);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<iframe name="myframe" id="frame1" src="contact_inputs.php">
<form id="iframe1Form">
<input type="" name="cn" value ="HELLO"/>
<input type="" name="ex" />
<input type="" name="cr" />
</form>
</iframe>
<form onsubmit="sub(event)">
<input type="submit" name="DoIt" value="submit" />
</form>
</body>
</html>
评论
0赞
apex anchor
11/18/2022
感谢@jerry提供这个解决方案,但对我不起作用,当我尝试用我的php_form.php提交表格时,我的邮件上收到了空消息,但是如果我在没有 iframe 的情况下提交表格,消息会显示在我的电子邮件上,所以 iframe :( 中的问题
评论