2016-06-08 52 views
0

我可以從表格創建動態/靈活複選框嗎? 我的表結構是這樣的:如何從數據庫中動態複選框?

| id | category | 
    1 School 
    2 Hospital 
    3 Police 

我複選框的html:

<input name="checkbox1" onclick="showPOI1($(this));" value="1" type="checkbox" id="checkbox" />School<br> 
    <input name="checkbox2" onclick="showPOI2($(this));" type="checkbox" id="checkbox" />Hospital <br> 
    <input name="checkbox3" onclick="showPOI3($(this));" type="checkbox" id="checkbox" />Police <br> 

和這樣每個複選框javascript函數:

 window.showPOI1 = function(t) { 
      if (t.is(':checked')) { 
       //alert('checked'); 
       for (i = 0; i < markers.length; i++) { 
        if (markers[i].category == 1) { //--> i want compare category from database, not hardcode 
         markers[i].setVisible(true); 
         markers[i].setAnimation(google.maps.Animation.BOUNCE); 


        } 

       } 

      } else { 
       infowindow.close(); 
       for (i = 0; i < markers.length; i++) { 
        if (markers[i].category == 1) { 
         markers[i].setVisible(false); 
        } 

       } 
      } 

     } 




     window.showPOI2 = function(t) { 
      if (t.is(':checked')) { 
       //alert('checked'); 
       for (i = 0; i < markers.length; i++) { 
        if (markers[i].category == 2) {//--> i want compare category from database, not hardcode 
         markers[i].setVisible(true); 
         markers[i].setAnimation(google.maps.Animation.DROP); 
        } 

       } 

      } else { 
       infowindow.close(); 
       for (i = 0; i < markers.length; i++) { 
        if (markers[i].category == 2) { 
         markers[i].setVisible(false); 
        } 

       } 
      } 

     } 



     window.showPOI3 = function(t) { 
      if (t.is(':checked')) { 

       for (i = 0; i < markers.length; i++) { 
        if (markers[i].category == 3) {//--> i want compare category from database, not hardcode 
         markers[i].setVisible(true); 
         markers[i].setAnimation(google.maps.Animation.DROP); 
        } 

       } 

      } else { 
       infowindow.close(); 
       for (i = 0; i < markers.length; i++) { 
        if (markers[i].category == 3) { 
         markers[i].setVisible(false); 
        } 

       } 
      } 

     } 

我有2個問題在這裏。首先,我想創建基於表的複選框。來自ID列的複選框值和來自類別列的標題。然後,我想將值發送給JavaScript函數。第二個問題是,我可以基於表創建動態函數嗎?可能嗎?或者你有另一個想法?如何從表中創建動態複選框和JavaScript函數。

在此先感謝:)

+0

您需要查詢表格,然後根據需要將結果輸出到DOM。 – chris85

+0

我用codeigniter,我正在查詢表並將值傳遞給視圖$ data ['category']。 –

+0

好的,當你輸出時會發生什麼? – chris85

回答

0

您可以使用這樣的,您可以通過使用數據庫創建動態複選框,ID傳遞給上的onclick複選框javascript函數。它可能會幫助你。

<?php 
$i=0; 
foreach($category as $row){ 
$i++; 
?> 
    <input name="checkbox[]" 
      onclick="showPOI1(<?php echo 'checkbox'.$i; ?>);" 
      value="<?php echo $row['category']; ?>" type="checkbox" 
      id="<?php echo 'checkbox'.$i; ?>" /> 

    <?php echo $row['category']; ?> 

<?php 
} 
?> 
+0

感謝您的回放,我會盡快嘗試。順便說一句,每個複選框都有一個功能。當我添加一個複選框時,我如何創建一個函數?或者你對我有建議或想法? –

+0

在這裏,點擊任何複選框,它將使用id參數轉到showPOI1的功能。你可以使用特定複選框的ID並做任何你想要的計算。 – Mani

+0

海馬尼,我想問一下,如果我用showPOI1($(this)),我可以得到這些複選框的值嗎? –