有人可以帮我弄清楚为什么我拥有的 json 数组没有填充我的选择下拉列表吗?该数组打印良好(名称:值),我尝试了不同的方式(.post .getJSON)来填充选择字段。请参阅下面的代码...
PHP
$q = "select Site_ID, FirstName, LastName from ClientInfo";
$sql = mysql_query($q);
$data = array();
while($row = mysql_fetch_array($sql, true)){
$data[] = $row;
};
echo json_encode($data);
这是它在网页上打印的内容:
[{"Site_ID":"10000001","FirstName":"drew","LastName":"0"}, {"Site_ID":"10000002","FirstName":"hello","LastName":"0"},{"Site_ID":"10000003","FirstName":"hihi","LastName":"0"},{"Site_ID":"10000004","FirstName":"","LastName":"0"},{"Site_ID":"10000005","FirstName":"SueAnn","LastName":"Hall"},{"Site_ID":"10000006","FirstName":"Test","LastName":"Name"}]
带有脚本的 HTML
<body>
<script type="text/javascript">
$(function(){
$.getJSON('checkin.php',function(data){
$.each(data,function(name,value)
{
items+="<option value='"+item.id+"'>"+item.name+"</option>";
});
$("#clients").html(items);
});
});
</script>
<select id="clients">
<option>Default</option>
</select>
</body>
在我运行 html 之前工作正常。预先感谢您的帮助。