简体   繁体   中英

How to generate DIV when the page on load using JQUERY?

<div id = "newdiv">
</div>    
<?php 
    $qry = "SELECT * from contact Where CustomerID='57'";
    $result = mysql_query($qry);
    while ($row = mysql_fetch_array($result)) {         
?>
<script type="text/javascript" language="javascript">
    function createDiv() {
        $('#newdiv').append('<div id="div"'+i+'" class="ex" style="text-align: left;"><div id="div"'+i+'" class= "square"><table border="0" class = "table" ><tr><td width="51">Name:</td><td width="141"><input type="text" size="10" class = "name"></td></tr><tr><td>Title:</td><td><input type="text" size="10" class = "title"></td></tr><tr><td>Contact:</td><td><input type="text" size="10"  class = "contact"></td></tr></table></div><img class="myimage" ondblclick="changeimage(this)" border="0"src="images/white_contact.png" width="60" /></div>');
        $( ".ex" ).draggable({containment:'parent',cursor:'pointer',opacity:0.6});
        $( ".ex" ).droppable();
    }
    createDiv(); // will execute when it loads this line
</script>
<?php
    }
?>

I wanted to Use a while loop of PHP to check the number of contact for Customer 57 for each Contact it find, it should generate one DIV, and so forth, and they should append to each other

My code Just not working, why is this so? I am using while loop to call the CreateDiv() function

$("body").append('<div id="foo">blabla</div>')

或者,如果您要替换现有div的内容。

$('#newdiv').html('..whatever..')

<script type="text/javascript" language="javascript">
    function createDiv() {
           $('#newdiv').append('<div id="div"'+i+'" class="ex" style="text-align:    left;">
                <div id="div"'+i+'" class= "square"><table border="0" class = "table" ><tr>
                <td width="51">Name:</td><td width="141"><input type="text" size="10" class = "name">
                </td></tr><tr><td>Title:</td><td><input type="text" size="10" class = "title">
                </td></tr><tr><td>Contact:</td><td><input type="text" size="10"  class = "contact">
               </td></tr></table></div><img class="myimage"    ondblclick="changeimage(this)" border="0"src="images/white_contact.png" width="60" />    </div>');

            $( ".ex" ).draggable({containment:'parent',cursor:'pointer',opacity:0.6});
            $( ".ex" ).droppable();
        }

   </script>
<div id = "newdiv">

</div>    

$qry = "SELECT * from contact Where CustomerID='57'";
$result = mysql_query($qry);
while ($row = mysql_fetch_array($result)) 
{
?>

   <script type="text/javascript" language="javascript">
      createDiv(); // will execute when it loads this line
   </script>

<?php
}
?>

PHP

<?php 
    $qry = "SELECT * from contact Where CustomerID='57'";
    $result = mysql_query($qry); 
    $count = mysql_num_rows($result);
    while ($row = mysql_fetch_array($result)) {  

    }       
?>

HTML

<html>
<head></head>
<body>
  <div id="newdiv"></div>
  <input type="hidden" name="count" value="<?=$count?>"/>
</body> 
</html>

JQUERY

<script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
   $(function() {
     createDiv();

      function createDiv(){
        var count = $('[name="count"]').val();
        for (var i=1;i<=count;i++) {
          $('#newdiv').append('<div id="div'+ i +'" class="ex" style="text-align: left;"><div id="div'+ i +'" class= "square"><table border="0" class = "table" ><tr><td width="51">Name:</td><td width="141"><input type="text" size="10" class = "name"></td></tr><tr><td>Title:</td><td><input type="text" size="10" class = "title"></td></tr><tr><td>Contact:</td><td><input type="text" size="10"  class = "contact"></td></tr></table></div><img class="myimage" ondblclick="changeimage(this)" border="0"src="images/white_contact.png" width="60" /></div>');
        }
      }


    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM