2012-02-01 51 views
1

使用Jquery UI“对话框”小部件我试图动态地创建多个模式窗口。动态创建多个Jquery UI模式对话框,php

在一个php循环中,我动态创建将触发模式打开的按钮,以及将作为模态窗口的div。在同一个循环中,我正在做一个回声并编写JavaScript来处理模态窗口。我的问题是我不知道如何处理写入JavaScript。目前我没有得到任何我的模式工作。我应该创建一个函数并通过每个循环迭代调用它,还是应该每次迭代发布整个document.ready例程?

<script type="text/javascript"> 

      function uiready(a){ 
    $("#" + a).dialog({ 
      autoOpen: false, 
      height: 650, 
      width: 625, 
      modal: true 
}); 
$("#Button" + a) 

      .click(function() { 
       $("#" + a).dialog("open"); 
      }); 

      }; 
     </script> 




    <?php 


       //This area should build details buttons if there is a program with an amount greater than $0 

       for ($i=1; $i<17; $i++){ 
        $ID= $_POST["textfield" . $i]; 

        if ($ID!=""){ 

         echo '<script type="text/javascript"> 

          uiready(' . $i . '); 
        </script>'; 

        //build a button with the ID equal the post value 
        echo "<input type=\"button\" value=\" $ID \" id=\"Button" . $i . "\"   class=\"btnmargin\" />"; 

        //next build the actual div 
        echo '<div class="printable' . $i . '" id="' . $i . '"> 
       <table cellspacing="0" cellpadding="10" border="2px"> 
       //for the sake of space I have not included the entire table markup 
       </table> 
     </div>'; 



        } 
       } 



       ?> 

回答

1

正如我看到的,你不需要将JavaScript与PHP混合来做你想做的事情。你只需要给“可打印的$ 1”div赋予相同的类名(例如“可打印”),然后用JS/jQuery来完成其余的工作。添加某物像这样的标记(未经测试):

<script type="text/javascript"> 
$('.printable').each(function() { 
    var dialog = $(this); 
    dialog.dialog({ 
     autoOpen: false, 
     height: 650, 
     width: 625, 
     modal: true 
    }); 
    dialog.prev().click(function() { 
     dialog.dialog("open"); 
    }); 
} 
</script> 
+0

不幸的是,这没有奏效。感谢您的帮助。我只是一起报废并使用[JQModal](http://dev.iceburg.net/jquery/jqModal/#how) – JDV590 2012-02-02 19:48:08