我在项目中的某个时候感到困惑。我在这里尝试做的事情如下:
从数据库中获取数据 使用按钮创建一个新的选择框 - 创建 使用数据库中的值填充此选择框 通过按钮删除该选择框的选项 - 删除
到目前为止我有 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,所以我不能像以前那样填充这个选择框。