0
    <p align="left">
        <b>Click this button to create div element dynamically:</b>

 <?php $qry = "SELECT COUNT(*)  AS count From Contact Where CustomerID=$pid";
$result = mysql_query($qry);
$row = mysql_fetch_array($result);
$count = $row['count'];
echo  $count;
if($count >= 6)
{
?>
    <input id="btn1" type="button" value="create div" onClick="popup();" />
<?php
}
else
{

  $num = 6 - $count ;   
  echo $num;
    ?>
<input id="btn1" type="button" value="create div" onClick="createDiv(<?php echo $num ?>);" />
<?php   
}


 ?>

我想将“$num”传递给我的创建 DIV 函数

如果 $num 为 6 则可以创建 6 个 div,如果 $num 为 4 则最多只能生成 4 个 div

这是我的 createDiv 函数

var i=0;
    function createDiv(num)
    {


  if(i < num) {
        var divTag = document.createElement("div");

        divTag.id = "div1"+i;

        divTag.setAttribute("align","left");

        divTag.style.margin = "0px auto";

        divTag.className ="ex";

       divTag.innerHTML = "<img class='myimage' onclick='changeimage(this)' border='0' src='images/white_contact.png' width='60'/><table border='0'><tr><td>Name:</td><td><input type='text'></d></tr><tr><td>Title:</td><td><input type='text'></td></tr><tr><td>Contact:</td><td><input type='text'></td></tr></table>";

        document.getElementById("newdiv").appendChild(divTag)

      }

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

    }

但是现在我只能创建一个DIV,为什么会这样?

4

1 回答 1

0

您可以稍微清理一下代码并使用处理程序来处理这种事情。

您似乎安装了 jQuery(因为您使用的是 droppable 和 draggable)

HTML:

<input type='button' class='div-creator' data-div-id="<? echo $id ?>" value="Click Me">

JAVASCRIPT:

$('body').on("click", '.div-creator', function() {
  var id = $(this).data("div-id");
  // And do whatever else you want to do here.
  // $(this) is the element that was clicked.  id is the div-id in the button tag.
});
于 2013-05-29T03:05:08.277 回答