使用输入类型text更改iframe-src

Change iframe src using input type text

本文关键字:更改 iframe-src text 类型 输入      更新时间:2024-03-22

我正在尝试使用输入中的url创建动态src chnge。这是我的代码:

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Web</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<form>
    <input type="text" id="id">
    <input type="button" value="Change site" onClick="newSite()">
</form>
    <iframe id="iframe" src="www.google.com"></iframe>
    <script type="text/javascript">
        function newSite() {
            var site = $('#id').val();
            document.getElementById('iframe').src = "http://" + site;
        }    
    </script>
</body>
</html>

所以我的问题是:如何通过按enter键发送表单?为什么它不起作用?

谢谢你的回答。

请在文本框中提供完整的url。

function newSite() {
  var site = $('#id').val();
  var iframe = document.getElementById('iframe');
  iframe.src = site;
  iframe.src = iframe.src;
}   
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Web</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
	<form>
	    <input type="text" id="id" />
	    <input type="button" value="Change site" onClick="newSite()" />
	</form>
  <iframe id="iframe" src="http://www.lipsum.com/" width="900" height='500'></iframe>
</body>
</html>