Javascript 在PHP中从JSON填充选择列表
我有一个数组Javascript 在PHP中从JSON填充选择列表,javascript,php,jquery,arrays,json,Javascript,Php,Jquery,Arrays,Json,我有一个数组 { "status": "OK", "message": "Data Show Successful", "allknife": [ { "name": "Folder", "notes": "" }, { "name": "Folder2", "notes": "" } } ] }
{
"status": "OK",
"message": "Data Show Successful",
"allknife": [
{
"name": "Folder",
"notes": ""
},
{
"name": "Folder2",
"notes": ""
}
}
]
}
我当前的代码运行正常
我当前的代码
<script>
$(document).ready(function() {
$("#person").change(function () {
var val = $(this).val()
if(val == "") return
$.getJSON("http://some.com/Knife/api/l.php?&returnformat=json", {"state":val}, function(res,code) {
$("#name").val(res.message)
$("#age").val(res.status)
})
})
})
</script>
现在我想从allknife数组中填充我的选择框值,每个数组名
像
这里的值0和1表示文件夹是allknife的第一个数组
当用户选择“人员”选择框时,表单的其他字段将使用“我的当前代码”填充;当用户选择第二个选择框0时,其他字段将填充
如果他们选择了文件夹,那么其他字段将从allknife填充到no array。
如果是folder2,则从allknife数组2填充其他字段
我的表格看起来像
<form>
<select name="person" id="person">
<input type="text" name="name" id="name"><br/>
<input type="text" name="age" id="age"><br/>
<select id="allknife"></select>
<input type="text" name="name" id="name"><br/>
<input type="text" name="note" id="note"><br/>
<input type="text" name="codskill" id="codskill"><br/>
</form>
您需要循环返回的数组。大概是这样的:
$.getJSON("http://some.com/Knife/api/l.php?&returnformat=json", { "state": val }, function(res, code) {
$("#name").val(res.message);
$("#age").val(res.status);
$("#codskill").val(res.allknife[0]['name']);
$.each(res.allknife, function(i, item) {
$('<option />', { text: i, value: item.name }).appendTo('#allknife');
});
})
另外,您的代码缺少一个结束标记。您可以发布您正在使用的代码以获得该结果吗。根据您问题中的代码,小提琴似乎起作用了。
$.getJSON("http://some.com/Knife/api/l.php?&returnformat=json", { "state": val }, function(res, code) {
$("#name").val(res.message);
$("#age").val(res.status);
$("#codskill").val(res.allknife[0]['name']);
$.each(res.allknife, function(i, item) {
$('<option />', { text: i, value: item.name }).appendTo('#allknife');
});
})