我对从ajax对象显示值有些怀疑。我需要使用ajax在选择框中显示名称。我不知道我在哪里做错了。我可以改变我的代码。请任何人帮助我。请提前谢谢。
我的javascript代码:
function getClient() {
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var message = xmlhttp.responseText;
var client = JSON.parse(message);
console.log(Object.keys(client));
document.forms["project_form"]["Project_cid"].innerHTML='<select class="form-control" maxlength="100" name="Project[cid]" id="Project_cid"><option value=Select>'+Object.keys(client)+'</option></select>'
}
}
var url = "/pms_pro/index.php/client/list";
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(null);
}
当我使用alert(消息)时,它将显示如下,
[{"cid":"1","name":"suresh","address":"Chennai"},{"cid":"2","name":"Ramesh","address":"Namakkal"},{"cid":"3","name":"Vignesh","address":"Trichy"}]
答案 0 :(得分:0)
这样的事情:
var select = document.getElementById('Project_cid');
// remove existing options
select.innerHTML = '';
// add the new options
var option, i;
for (i = 0; i < client.length; i++) {
option = document.createElement('option');
option.text = client[i].name;
option.value = client[i].cid;
select.add(option);
}