有点混乱如何访问员工和经理阵列的ID号JSON变量中的ID号。
var json = {
"employee": ["6"],
"manager": ["12", "78"]
};
我会用它来比较,如果有员工和管理器中数据元素阵列匹配员工和经理的JSON身份证号码这一点。
编辑:
$('#theForm').on('submit', function(e){
e.preventDefault();
$param = $('#name').val();
$.ajax({
type: 'POST',
data: {
'param': $param
},
url: 'getResults.php',
success: function(json) {
// alert(json); //works. outputs entire json
alert(json.employee[0]); // undefined
var employee = json.employee;
if (employee && employee.length) {
alert(employee[0]); // doesn't show up
}
}
});
});
EDIT2:
$theArray = null;
$results = mysql_query("...QUERY...");
if($results){
$employee = array();
while($row = mysql_fetch_array($results)){
array_push($employee, $row['id']);
}
$theArray['employee'] = $employee;
}else{
die ('Can\'t do that: ' . mysql_error());
}
// repeat for manager
...
// close connection
echo json_encode($theArray);
你拼错'employee'在小提琴 – Musa
请保持您的浏览器开发者控制台打开在测试期间。你会立即发现问题。 'TypeError:json.employe未定义' –
修复了错字。仍然不起作用。 – user1701467