如何在使用JQUERY加载页面时生成DIV?

时间:2013-06-04 09:30:22

标签: php jquery html

<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
    }
?>

我想使用PHP的while循环来检查Customer 57的联系人数量 对于它找到的每个联系人,它应该生成一个DIV,依此类推,它们应该相互追加

我的代码只是不工作,为什么会这样?我正在使用while循环来调用CreateDiv()函数

3 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

<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
}
?>

答案 2 :(得分:0)

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>