首页 > 解决方案 > 如何将表单数据发送到同一网页中的 iframe src

问题描述

我有一个带有一个输入的表单,您可以在其中键入网站的 URL,然后 URL 应该被发送到同一网页上的 iframe,它应该 iframe 的 URL。这是我当前在URL 输入中键入http://example.com时得到的代码,它给了我错误,cannot GET /my_frame%22?Url=http%3A%2F%2Fexample.com

    <html>
      <head>
        <title>Form Iframe Demo</title>
      </head>
      <body>

       <form action=my_frame" >
        <input type="text" name="Url" value="http://" />
        <input type="submit" />
      </form>
<--Iframe should receive url from the form and then Iframe it -->
        <iframe name="my_frame" src="Url" width="860px">
       </iframe>
      </body>
    </html>

试过搞砸它,但它让我陷入困境

我希望它有一个http://example.com的 iframe,但我收到一个错误

标签: htmlformsiframe

解决方案


<html>
<head>
        <title>Form Iframe Demo</title>
      </head>
      <body>      
        <input type="text" name="Url" id="url" value="https://j-mrad.github.io/" />
        <button onClick="LoadURL();" >Load URL</button>
        <br><br>       
        <iframe id="my_frame" src="https://www.w3schools.com" width="600px" height="500px">
        </iframe>       
       <script type="text/javascript">
       function LoadURL(){
       document.getElementById("my_frame").src=document.getElementById("url").value;
       }

       </script>

      </body>
    </html>

推荐阅读