Javascript 使用document.getElementById().innerHTML发出警报字符串

Javascript 使用document.getElementById().innerHTML发出警报字符串,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我试图使用document.getElementById().innerHTML警告字符串,但它警告的是整个页面的代码,而不是div中的字符串。我需要它来警告“121”。我做错了什么 <script type = "text/javascript"> function getUrban(pageNum, stopAt, gotoUrl) { var currentPage = "currentPage"; $.ajax({ type: "POST",

我试图使用document.getElementById().innerHTML警告字符串,但它警告的是整个页面的代码,而不是div中的字符串。我需要它来警告“121”。我做错了什么

<script type = "text/javascript"> 

function getUrban(pageNum, stopAt, gotoUrl) {

var currentPage = "currentPage";
$.ajax({
           type: "POST",
           url: gotoUrl,
           data: { pageNum : pageNum },
           error: function(xhr,status,error){alert("error");},
           success:function(data) {
           document.getElementById("output").innerHTML = data;
           currentPage = document.getElementById("output").innerHTML;
           },
           complete:function(data) {
           alert(currentPage);
           } //end of complete:function(data)

      });

} //end of function getUrban(pageNum)

getUrban(121,422,"test.php");

</script>

<div id = "output"></div>
id为“Output”的div中的输出:

需要提醒:

test.php


既然您使用jQuery来实现ajax,那么为什么不在其他方面也使用jQuery呢

$('#output').text();
将只获取div中的文本,而不获取html元素

看到人们使用jQuery并在代码中包含document.getElementById('id'),我简直要疯了。您确实意识到,$('id')将获得相同的元素,但是使用jQuery包装器,因此您可以对其使用jQuery函数。而且打字要短得多

<script type = "text/javascript"> 

function getUrban(pageNum, stopAt, gotoUrl) {

$.ajax({
           type: "POST",
           url: gotoUrl,
           data: { pageNum : pageNum },
           error: function(xhr,status,error){alert("error");},
           success:function(data) {
               $("#output").html(data);
           },
           complete:function(data) {
               alert($('#output').text());
           } //end of complete:function(data)

      });

} //end of function getUrban(pageNum)

getUrban(121,422,"test.php");

</script>

<div id = "output"></div>

函数getUrban(pageNum、stopAt、gotour){
$.ajax({
类型:“POST”,
网址:Gotour,
数据:{pageNum:pageNum},
错误:函数(xhr,status,error){alert(“error”);},
成功:功能(数据){
$(“#输出”).html(数据);
},
完成:功能(数据){
警报($('#output').text());
}//完成结束:函数(数据)
});
}//函数getUrban(pageNum)的结尾
getUrban(121422,“test.php”);

@andreCoder警报已完成。您是否可以添加
打印($\u POST)
到您的test.php并在此处发布输出?@AndrewCoder也很抱歉。谢谢你们的帮助,不用说对不起。好消息是问题已经解决了。不用担心。加载的内容是文本还是整个html文档?您能在这里发布加载的内容吗?ajax调用中的
数据
对象是呈现的html或其他我们看不到的代码。我们不知道输出的是网页还是数字121。@GreggDuncan这只是数字121。你确定加载的是这个吗。尝试执行警报(数据);在你的成功功能中。因为如果它只是在输出div中加载数字121,那么alert($('#output').text());将通知号码121。就这样,
121
$pageNum = $_POST['pageNum'];
echo $pageNum;
$('#output').text();
<script type = "text/javascript"> 

function getUrban(pageNum, stopAt, gotoUrl) {

$.ajax({
           type: "POST",
           url: gotoUrl,
           data: { pageNum : pageNum },
           error: function(xhr,status,error){alert("error");},
           success:function(data) {
               $("#output").html(data);
           },
           complete:function(data) {
               alert($('#output').text());
           } //end of complete:function(data)

      });

} //end of function getUrban(pageNum)

getUrban(121,422,"test.php");

</script>

<div id = "output"></div>