首页 > 解决方案 > 我需要一个 AJAX 脚本来加载提交时在输入中输入的 URL

问题描述

我做了类似的事情,但不是我想要的。我需要的是加载一个正在输入的 url。例如。我输入“001.xml”并提交它,这就是要加载的内容。

<!DOCTYPE html>
<html>
<head>
<script>
  src="https://code.jquery.com/jquery-3.4.1.js"
  integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
  crossorigin="anonymous"></script>
<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "", true);
  xhttp.send();

}
</script>
</head>
<body>

<div id="demo">
  <h2>Let AJAX change this text</h2>

</div>
  <input type="text" id="url"></input>
  <button type="button" onclick="loadDoc()">Change Content</button>
</body>
</html>

标签: jqueryhtmlajax

解决方案


你要加载什么?您现有页面的外部 URL 或一些查询字符串?此外,您可能必须更改<script>加载 jQuery 的标签。src 应该在脚本标签内。


推荐阅读