how to get div's text to a variable
what I mainly want to do is to get the div's content and pass it in a variable. To explain what I have done until now :
I have my php file that contains the code :
<?php
$connect = mysql_connect("localhost", "...","...") or die("Could not connect to the database.");
    mysql_select_db("...") or die("Could not find database <...>");
    $query = mysql_query("SELECT id FROM datainput WHERE id>=ALL(SELECT id FROM datainput)") or die("Query could not be executed");
    $row = mysql_fetch_assoc($query);
    echo开发者_开发知识库 $row['id'];
?>
In my index.php file I have written the following script :
 <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            $("document").ready( function(){
                //setInterval("checkForNewData()", 1000);  //30 Minutes = 1800000 Milliseconds
                checkForNewData();
            });
            function checkForNewData(){
                $("#lastid").load("lastData.php");
                var mydata = $("#lastid").text();
            }  
        </script>
In my html tag I have a div with id="lastid". With the code below :
 var mydata = $("#lastid").text();
I want to keep the current text of lastid div, so I can later compare it with another.
As I have read here, this should have done mydata="6" (is the current result)? What am I doing wrong?
Can anyone help? Pleaseee...
You need to wait for the load to have finished. This is done by using a callback function like so:
   <script type="text/javascript">
        $("document").ready( function(){
            //setInterval("checkForNewData()", 1000);  //30 Minutes = 1800000 Milliseconds
            checkForNewData();
        });
        function checkForNewData(){
            $("#lastid").load("lastData.php", function(){
                var mydata = $("#lastid").text();
            });
        }  
    </script>
Please see the jQuery API docs for more information: http://api.jquery.com/load/
Essentially, the second argument of the load function can be a function. If it is, then whatever code is in that function will be executed when the load has completed.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论