簡體   English   中英

從MySQL數據庫中填充動態添加的選擇框

[英]Populate dynamically added select boxes from MySQL Database

我在項目中的某些方面感到困惑。 我想在這里做的事情如下:

從數據庫中獲取數據使用按鈕創建一個新的選擇框 - 創建使用數據庫中的值填充此選擇框選項以通過按鈕刪除該選擇框 - 刪除

我到目前為止的代碼PHP / HTML:

   <button type="button" name="add" onclick="return false;" id="addField" class="nice_button">Добавить(ADD)</button>


   <button type="button" name="remove" onclick="return false;" id="remove" class="nice_button">Удалить(DELETE)</button>


 <tbody id="documentFields">
 <tr>
    <td>
        <select size="1" name="hardware[]" style='width:400px;'>
        <option value=""></option>
        <?php   while($hardware = $get_hardware->fetch_array()){
          echo "<option  value='".$hardware['st_id']."'>".$hardware['st_name']." ".$hardware['st_producer']." ".$hardware['st_model']."</option>";
        }
        ?>
        </select>
    </td>
</tr>
</tbody>

到目前為止,我的JS腳本看起來像這樣:

var g = 1;
var id = [ <? php echo $js_id; ?> ];
$(document).ready(function Product_add() {
    $("#addField").click(function () {
        $("#documentFields").append('<tr id="fieldset_p' + g + '"><td><select size="1" name="hardware[]" style="width:400px;"><option value=""></option></select></td></tr>');
        g++;
    });
});
$(document).ready(function Product_add() {
    $('#remove').click(function () { // similar to the previous, when you click remove link
        if (g > 1) { // if you have at least 1 input on the form
            g--; //deduct 1 from i so if i = 3, after i--, i will be i = 2
            $('#fieldset_p' + g + '').remove(); //remove the last fieldset  
        }
    });
});

問題是我無法附加php函數:while,所以我無法將此選擇框填充為之前的那個。

在這種情況下,您可以使用Ajax。

什么是ajax:

http://en.wikipedia.org/wiki/Ajax_(programming)

它以特定格式提供僅包含您信息的頁面。 然后用你的javascript調用它,然后將其解析到頁面中。

文檔如何在jquery中執行此操作:

http://api.jquery.com/jQuery.ajax/


更新:

代碼示例:

$.get('ajax/test.html', function(data) {

    // read in data = = [{'option':'option 1'},{'option':'option 2'}]

      $.each(data, function(index, itemData) {
       /// do stuff
         $('<option>' + itemData.option + '</option>').appendTo('#myselectbox');
      });

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM