我正在开发一个网络应用程序。我正在从数据库中检索数据并使用$row = mysqli_fetch_array(). 问题是,这个数组应该去ajax使用,echo json_encode($array)但我不知道如何从 ajax 成功函数接收它,顺便说一句,我正在使用jQuery.parseJSON(). 之后,我将把该数组中的值附加到一个<option>元素上。get_AssetModel.php:$sqlGetAssetModels = "select * from tblbrands where brand_name like '$phpGetAssetModels'";//$array = array();$models = array();if ($result = mysqli_query($cn, $sqlGetAssetModels)) { if (mysqli_num_rows($result) > 0) { $row = mysqli_fetch_array($result); $models[] = $row[3]; }} else {}echo json_encode($models);将接收数组的 javascript 文件:$.ajax({ url: "inventory/get_AssetModel.php", type: "POST", data: chAssBrand, success: function(data, textStatus, jqXHR){ var data = jQuery.parseJSON(data); var node = document.createElement("option"); var textnode = document.createTextNode(data.0); node.appendChild(textnode); document.getElementById("txtInvModel").appendChild(node); }, error: function(jqXHR, textStatus, errorThrown){ swal("Error!", textStatus, "error"); } });或者是他们的其他方式?
1 回答

陪伴而非守候
TA贡献1757条经验 获得超8个赞
我假设你data可能看起来像下面的例子:
var data = ["Anthony", "Jhake", "Ravelo", "Antigro"];
data.forEach(function(value, index) {
var option = document.createElement("option");
option.text = value;
option.value = index;
var select = document.getElementById("txtInvModel");
select.appendChild(option);
});
<select name="txtInvModel" id="txtInvModel">
<option value="">Select one</option>
</select>
- 1 回答
- 0 关注
- 237 浏览
添加回答
举报
0/150
提交
取消