2011-08-26 32 views
0

我的代碼:爲什麼jquery沒有更新我的變量<span>?

if ($numOfMessages <> 0) 
{ 
    echo "<span class='headings_sub' id='msgcntDiv'>You have " .$numOfMessages . "</span>"; 
    echo "<a class='red_link' href='".ADDRESS."messages.php'> unopened Messages</a>"; 
} 

的Jquery:

<script> 
    $(document).ready(function() { 
     refresh(); 
    }); 

    function refresh() 
    {  
     $.get('newMessageCnt.php', function (cnt) { 
     $("#msgcntDiv").data('cnt', cnt); 
     window.setTimeout(refresh,30000); 
     }); 
    } 
</script> 

newMessageCnt.php:

<?php 
include('header_application.php'); 
$obj_clean->check_user(); //$obj_clean defined/initialized in header_application 
echo $obj_clean->getUnopenedMessagesCount($_SESSION['user_id']); 
?>  

任何意見嗎? 謝謝

回答

0

您正在向元素添加數據而不是更改其內容。嘗試:

$("#msgcntDiv").text(cnt); 

相反的:

$("#msgcntDiv").data('cnt', cnt); 
1
function refresh() 
    {  
     $.get('newMessageCnt.php', function (cnt) { 
     $("#msgcntDiv").html(cnt); 
     window.setTimeout(refresh,30000); 
     }); 
    } 

不使用data(),使用html()

2
function refresh() 
{  
    $.get('newMessageCnt.php', function (cnt) { 

    $("#msgcntDiv").html("You have "+cnt); 

    window.setTimeout(refresh,30000); 
    }); 
} 
相關問題