此代码片段可以让你将数据提交给iframe,不使用任何js,也不需要跳转即可直接提交。
代码
<form action="https://www.codeku.run/?s=php" target="iframe" method="get"> <label for="text">输入内容:</label> <input type="text" name="s" id="text"> <input type="hidden" name="type" value="post"> <input type="submit" value="发布"> </form> <iframe name="iframe" src="https://www.codeku.run"></iframe>
实现效果如下:

完整源代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title></title> <link rel="stylesheet" href=""> <style> iframe{ width: 100%; height: 100vh; } </style> </head> <body> <form action="https://www.codeku.run/?s=php" target="iframe" method="get"> <label for="text">输入内容:</label> <input type="text" name="s" id="text"> <input type="hidden" name="type" value="post"> <input type="submit" value="发布"> </form> <iframe name="iframe" src="https://www.codeku.run"></iframe> </body> </html>
原理讲解
表单的target设置为iframe(为iframe名称),则会将数据提交给iframe。
注意:iframe的name属性必须与form的target属性一致。