0

我在项目中的某个时候感到困惑。我在这里尝试做的事情如下:

从数据库中获取数据 使用按钮创建一个新的选择框 - 创建 使用数据库中的值填充此选择框 通过按钮删除该选择框的选项 - 删除

到目前为止我有 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,所以我不能像以前那样填充这个选择框。

4

1 回答 1

2

在这种情况下,您可以使用 Ajax。

什么是ajax:

http://en.wikipedia.org/wiki/Ajax_(编程)

它以特定格式提供仅包含您的信息的页面。然后用你的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');
      });

});
于 2013-02-06T09:07:08.707 回答