【问题标题】:jquery function not working inside a javascript, but works great outside itjquery 函数不能在 javascript 内部工作,但在其外部工作得很好
【发布时间】:2012-06-12 19:00:11
【问题描述】:

我有一个包含 jQuery 和 script.js 文件的 php 页面。当我将以下函数放入我的 php 文件中时,该函数将被执行并按预期工作:

<script>
    $('#scoreboard-overview').load('getusers.php?q=<?php echo $NewString; ?>').fadeIn("slow"); 
</script>

它有什么作用?好吧,它会在我的 php 文件中重新加载 id 为 scoreboard-overview 的 div,其中包含来自名为 getusers.php 的外部 php 文件的数据

这一切都很好。

现在在 script.js 文件中(在 php 页面的末尾加载,就在 &lt;/body&gt; 之前),我还想在 updatecore.php 文件完成后通过表单更新数据库时重新加载这个 div .我有这个代码:

$.ajax({
    type: "POST",
    data: $("#form").serialize(),
    cache: false,
    url: "updatescore.php",
    success: function() { //reload overview
        $('#scoreboard-overview').load('getusers.php?q=' + document.getElementById('str') + '').fadeIn("slow");
    }
});​

所以成功后应该执行jQuery函数:

function () { //reload overview
    $('#scoreboard-overview').load('getusers.php?q='+document.getElementById('str')+'').fadeIn("slow");                  
}

我验证了代码。使用updatescore.php 更新数据库,但在此之后(成功后),div 不会刷新。所以代码的这个成功部分不起作用。我做错了什么?

ps:'+document.getElementById('str')+' 应该给我与 echo $NewString; 相同的结果,只取自 id 为 str 的 div(因为 php 在 js 文件中不起作用?)

【问题讨论】:

  • 您页面上的元素是否存在id="str"
  • document.getElementById('str') 将返回带有 'str' id 的 HTML 元素,而不是元素中的文本。
  • @sczizzo 这应该是一个答案而不是评论。
  • @JamesMontagne 好点。已添加。

标签: javascript jquery ajax jquery-load


【解决方案1】:

document.getElementById('str') 将返回带有 'str' id 的 HTML 元素,而不是元素中的文本。我不确定这个元素实际上是什么样子,但你可能会做类似document.getElementById('str').textContent 的事情。

【讨论】:

  • 啊啊我用document.getElementById('str').innerHTML,这似乎工作......谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多